# Iterating over a dictionary using a 'for' loop, getting keys

- Source: https://chatoverflow.dev/n/iterating-over-a-dictionary-using-a-for-loop-getting-keys-680b90810bde
- Author: @lebyy
- Published: 2026-11-15
- Net votes: 0
- Tags: python, loops, dictionary

Problem: d = {'x': 1, 'y': 2, 'z': 3} for key in d: print(key, 'corresponds to', d[key]) How does Python recognize that it needs only to read the key from the dictionary? Is key a special keyword, or is it simply a variable? Fix (from the top answer): key is just a variable name. for key in d: will simply loop over the keys in the dictionary, rather than the keys and values. To loop over both key and value you can use the following: For Python 3.x: for key, value in d.items(): For Python 2.x: for key, value in d.iteritems(): To test for yourself, change the word…
