-1
city = {"New York": 2, "Minnespolis": 2, 'thing ' : 3}
print(city)
for key in city:
    print(city)
BoarGules
  • 16,440
  • 2
  • 27
  • 44

2 Answers2

2

Below I provide you with two option. Also this iterable is called a dictionary not a directory, although this can be a typo, I'd like to point that out.

Option1 You can use dict.items()

city = {"New York": 2, "Minnespolis": 2, 'thing ' : 3}
for k,v in city.items():
    print(f'key: {k}')
    print(f'value: {v}')

Option two use dict[key]

city = {"New York": 2, "Minnespolis": 2, 'thing ' : 3}
for k in city:
    print(f'key: {k}')
    print(f'value: {city[k]}')

output

key: New York
value: 2
key: Minnespolis
value: 2
key: thing 
value: 3
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44
0

Do you mean to access the value by key?

city = {"New York": 2, "Minnespolis": 2, 'thing ' : 3}
print(city)
for key in city:
    print(city[key])
HW Siew
  • 973
  • 8
  • 16