Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary question
#1
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?
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks...can I not do this just using dictionary rather than a list of dictionary? I am specifically trying to learn about dictionaries here.
Reply
#4
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')
Reply
#5
(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.
Reply
#6
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]
Reply
#7
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.
Reply
#8
What are you having trouble understanding? Have you not seen functions before?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020