Iterating over dictionaries using 'for' loops
Asked 07 September, 2021
Viewed 1.9K times
  • 57
Votes

I am a bit puzzled by the following code:

d = {'x': 1, 'y': 2, 'z': 3} 
for key in d:
    print (key, 'corresponds to', d[key])

What I don't understand is the key portion. How does Python recognize that it needs only to read the key from the dictionary? Is key a special word in Python? Or is it simply a variable?

14 Answer