Python Forum
Line of code to show dictionary doesn't work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Line of code to show dictionary doesn't work
#1
Hi,

I am trying to write a line of code that should return the names and their favourite colors. Somehow it won't work. What am i doing wrong? I don't get an error messgae just nothing happens when i press enter after the code.

Thanks in advance!

>>> Colors = {"Sam": "Blauw", "Ali": "Rood", "Saar": "Geel"}
>>> Colors
{'Sam': 'Blauw', 'Ali': 'Rood', 'Saar': 'Geel'}
>>> Colors.keys()
dict_keys(['Sam', 'Ali', 'Saar'])
>>> for Item in Colors.keys():
	print("{0} houdt van de kleur {1}."
	      .format(Item, Colors{Item}))
	
SyntaxError: invalid syntax
>>> for Item in Colors.keys():
	print("{0} houdt van de kleur {1}."
	      .format(Item, Colors[Item]))
Reply
#2
Did you forget to indent the print inside the for? Also, a few things:

1. Dictionaries are iterable and iterating over them gives you the keys, so calling keys explicitly is unnecessary.

2. If you want both the key and the value, you can use items:

>>> d = {"foo": 1, "bar": 2}
>>> for key, value in d.items():
...     print(key, value)
... 
foo 1
bar 2
Reply
#3
Use Colors['Sam'] to get Sam's favorite color. You can also use Colors.get('Sam'). get is useful when looking up keys that may not be in the dictionary. Colors['Bob'] will throw an exception, but Colors.get('Bob', 'Green') will return 'Green', a default value used when the key is not found.

This code can use [] because the keys are guaranteed to be in the dictionary.
Colors = {'Sam': 'Blauw', 'Ali': 'Rood', 'Saar': 'Geel'}

for Item in Colors:
    # print("{0} houdt van de kleur {1}.".format(Item, Colors[Item])) # Works
    print(f'{Item} houdt van de kleur {Colors[Item]}.')) # Works, shorter, reads better
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PIP doesn't work YKR 1 682 Mar-28-2025, 02:10 PM
Last Post: snippsat
  I'm trying to install python 3.11.11 on windows 10 - it doesn't work Petonique 2 1,838 Feb-04-2025, 05:42 PM
Last Post: snippsat
  Can't get graph code to work properly. KDDDC2DS 1 655 Sep-16-2024, 09:17 PM
Last Post: deanhystad
  Printing the code line number arbiel 6 1,646 Jun-30-2024, 08:01 AM
Last Post: arbiel
  I can't for the life of me get this basic If statement code to work CandleType1a 8 2,253 May-21-2024, 03:58 PM
Last Post: CandleType1a
  Extending list doesn't work as expected mmhmjanssen 2 1,399 May-09-2024, 05:39 PM
Last Post: Pedroski55
  Unable to understand the meaning of the line of code. jahuja73 0 1,031 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  hi need help to make this code work correctly atulkul1985 5 1,943 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 1,538 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 1,932 Jun-24-2023, 02:14 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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