Python Forum
Dictionary question - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: Homework (https://python-forum.io/Forum-Homework)
+--- Thread: Dictionary question (/Thread-Dictionary-question--31075)



Dictionary question - kam_uk - Nov-21-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?


RE: Dictionary question - buran - Nov-21-2020

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.


RE: Dictionary question - kam_uk - Nov-21-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.


RE: Dictionary question - deanhystad - Nov-21-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')



RE: Dictionary question - perfringo - Nov-21-2020

(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)


RE: Dictionary question - kam_uk - Nov-21-2020

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]


RE: Dictionary question - deanhystad - Nov-21-2020

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.


RE: Dictionary question - ndc85430 - Nov-22-2020

What are you having trouble understanding? Have you not seen functions before?