Posts: 17
Threads: 10
Joined: Sep 2020
Hey All
I have a dictionary as such
car_dictionary = {
"Ford" : "Blue",
"Nissan" : "Red"
} I want to assign a parameter (or is it argument) to the key/value so they are labelled as "make" and "color"
That way I can maybe print out something like "The Nissan is red"
How can I do this?
Posts: 8,089
Threads: 154
Joined: Sep 2016
Nov-21-2020, 07:26 AM
(This post was last modified: Nov-21-2020, 07:26 AM by buran.)
it's neither parameter nor argument
Unless you want to make a more complex data structure, what you want, as I understand it is
cars = {"Ford" : "Blue", "Nissan" : "Red"}
# loop over all cars
for car, color in cars.items():
print(f'The {car} is {color}')
# or directly access the value for specific key
car = 'Nissan'
color = cars.get(car, 'unknown')
print(f'The {car} is {color}') a more complex data structure (list of dicts)
cars = [{'make':"Ford", 'color':"blue"}, {'make':"Nissan", 'color':"red"}]
for car in cars:
print(f"The {car['make']} is {car['color']}") You can use different data structure, e.g. namedtuple, instead of dict or write your own class, etc.
Posts: 17
Threads: 10
Joined: Sep 2020
Thanks...can I not do this just using dictionary rather than a list of dictionary? I am specifically trying to learn about dictionaries here.
Posts: 6,385
Threads: 17
Joined: Feb 2020
def car(make, model, style, year, color):
return {'make':make, 'model':model, 'style':style, 'year':year, 'color':color}
cars = {'fun car':car('Chevrolet', 'Corvette', 'coupe', 2020, 'red'),
'work car':car('Ford', 'F150', 'truck', 1990, 'rust'),
'dad van':car('Chrysler', 'Pacifica', 'minivan', 2015, 'white')}
for label, car in cars.items():
print(label, '\n', car, '\n')
Posts: 1,939
Threads: 8
Joined: Jun 2018
Nov-21-2020, 05:19 PM
(This post was last modified: Nov-21-2020, 05:20 PM by perfringo.)
(Nov-21-2020, 12:33 PM)kam_uk Wrote: can I not do this just using dictionary rather than a list of dictionary? I am specifically trying to learn about dictionaries here.
buran pointed out possibility: 'unless you want to make a more complex data structure'. You can have dictionary which value is dictionary as well. This way you can provide all the features with key, somthing along those lines:
>>> features = ['model', 'year', 'color']
>>> data = dict()
>>> data['Ford'] = dict(zip(features, ['Mustang', 2020, 'red']))
>>> data['Volvo'] = dict(zip(features, ['X90', 2020, 'white']))
>>> data
{'Ford': {'model': 'Mustang', 'year': 2020, 'color': 'red'},
'Volvo': {'model': 'X90', 'year': 2020, 'color': 'white'}}
>>> data['Ford']['model']
'Mustang' However, you probably see problem with that approach - you can have only one model per manufacturer as keys in dictionary must be unique. Therefore for similar records it is simpler to have list of dictionaries (or named tuples) and not introduce another layer of complexity to overcome key uniqueness requirement (value of key could be list of dictionaries for example where every model is one dictionary)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 17
Threads: 10
Joined: Sep 2020
Nov-21-2020, 08:18 PM
(This post was last modified: Nov-21-2020, 08:18 PM by kam_uk.)
Thanks for all the response!
The below looks interesting, could someone explain the code?
[python]
def car(make, model, style, year, color):
return {'make':make, 'model':model, 'style':style, 'year':year, 'color':color}
cars = {'fun car':car('Chevrolet', 'Corvette', 'coupe', 2020, 'red'),
'work car':car('Ford', 'F150', 'truck', 1990, 'rust'),
'dad van':car('Chrysler', 'Pacifica', 'minivan', 2015, 'white')}
for label, car in cars.items():
print(label, '\n', car, '\n') [/python]
Posts: 6,385
Threads: 17
Joined: Feb 2020
Nov-21-2020, 08:42 PM
(This post was last modified: Nov-21-2020, 08:43 PM by deanhystad.)
No. Figure it out yourself. Call the car() function and explore what gets returned. Try putting two cars in a dictionary.
Ok, one hint. The {} is a Dictionary constructor. But you already knew that.
Posts: 1,823
Threads: 2
Joined: Apr 2017
What are you having trouble understanding? Have you not seen functions before?
|