Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary question
#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


Messages In This Thread
Dictionary question - by kam_uk - Nov-21-2020, 02:07 AM
RE: Dictionary question - by buran - Nov-21-2020, 07:26 AM
RE: Dictionary question - by kam_uk - Nov-21-2020, 12:33 PM
RE: Dictionary question - by perfringo - Nov-21-2020, 05:19 PM
RE: Dictionary question - by deanhystad - Nov-21-2020, 03:01 PM
RE: Dictionary question - by kam_uk - Nov-21-2020, 08:18 PM
RE: Dictionary question - by deanhystad - Nov-21-2020, 08:42 PM
RE: Dictionary question - by ndc85430 - Nov-22-2020, 05:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  dictionary question stereokim123 2 40,002 Apr-01-2021, 10:23 PM
Last Post: stereokim123

Forum Jump:

User Panel Messages

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