Python Forum

Full Version: Dictionary question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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.
Thanks...can I not do this just using dictionary rather than a list of dictionary? I am specifically trying to learn about dictionaries here.
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')
(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)
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]
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.
What are you having trouble understanding? Have you not seen functions before?