Not using items()
to iterate over a dictionary¶
PEP 20 states “There should be one– and preferably only one –obvious way to do it.” The preferred way to iterate over the key-value pairs of a dictionary is to declare two variables in a for loop, and then call dictionary.items()
, where dictionary
is the name of your variable representing a dictionary. For each loop iteration, Python will automatically assign the first variable as the key and the second variable as the value for that key.
Anti-pattern¶
The code below defines a for loop that iterates over a dictionary named d
. For each loop iteration Python automatically assigns the value of key
to the name of the next key in the dictionary. Inside of the for
loop the code uses key
to access the value of each key of the dictionary. This is a common way for iterating over a dictionary, but it is not the preferred way in Python.
d = {"first_name": "Alfred", "last_name":"Hitchcock"}
for key in d:
print("{} = {}".format(key, d[key]))
Best-practice¶
Use items()
to iterate across dictionary¶
The updated code below demonstrates the Pythonic style for iterating through a dictionary. When you define two variables in a for
loop in conjunction with a call to items()
on a dictionary, Python automatically assigns the first variable as the name of a key in that dictionary, and the second variable as the corresponding value for that key.
d = {"first_name": "Alfred", "last_name":"Hitchcock"}
for key,val in d.items():
print("{} = {}".format(key, val))
Difference Python 2 and Python 3¶
In python 2.x the above examples using items
would return a list with tuples containing the copied key-value pairs of the dictionary. In order to not copy and with that load the whole dictionary’s keys and values inside a list to the memory you should prefer the iteritems
method which simply returns an iterator instead of a list.
In Python 3.x the iteritems
is removed and the items
method returns view objects. The benefit of these view objects compared to the tuples containing copies is that every change made to the dictionary is reflected in the view objects.