Python dict difference between items and iteritems
Introduction
Welcome to a new code snippet in Python. Today, we are going to clarify the difference between items() and iteritems() methods when using a Python dictionary. Let us first quickly refresh our memory about lists, tuples and dictionaries in Python:
- A list is an array data structure that can be edited
- A tuple is an immutable (i.e. cannot be edited) array
- A dictionary (i.e. hash table) is a list of key value pairs. It is a fast data structure (O(1) running time)
Here are few examples:
1 2 3 4 5 6 |
L = [1, 2, 3] T = (1, 2, 3) D = {1:'x', 2:'y'} print(type(L)) print(type(T)) print(type(D)) |
The output will be
1 2 3 |
<type 'list'> <type 'tuple'> <type 'dict'> |
Let us now proceed with the comparison. You have probably encountered Python code using items(), iteritems() and viewitems(). This is confusing for sure, so what is the difference between all these methods? The root cause of all that confusion is not realizing which Python version we are referring to. It is a historical issue that started in Python 2.x and got improved in Python 3.x.
Let us see how…
Python 2.x
Python 2.x has the dictionary method items() which returns a list of tuples. Here is how a list of tuples looks like:
1 2 |
L = [(1, 2), (2, 3), (3,7)] print(type(L)) |
The output will be
1 |
<type 'list'> |
If the dictionary is large in size, the returned list requires more memory space which is not an efficient implementation. A better way to do it is to use iterators. Iterators is beyond the scope of this article. For more information about that, take a look at the following post. Using iterators, a new method was introduced that is iteritems() and the old method items() was kept for backward compatibility. So iteritems() is better however we cannot use it while adding or deleting entries from the dictionary. For that reason, Python 2.6 introduced a new method called viewitems() which returns a dynamic view of the dictionary key value pairs. It allows us to traverse the dictionary and edit at the same time. As you can see, we ended up with three methods.
That is confusing! Python 3 makes our life easy, let us see that…
Python 3.x
In Python 3, there is only one method named items(). It uses iterators so it is fast and allows traversing the dictionary while editing. Note that the method iteritems() was removed from Python 3. Let us take some examples:
Python 2.x examples
- Example 1
1 2 3 |
d = {1: 'x', 2: 'y'} I = d.items() print(I) |
This will print all tuples in the dictionary as a list
1 |
[(1, 'x'), (2, 'y')] |
- Example 2
1 2 3 4 5 |
d = {1: 'x', 2: 'y'} I = d.iteritems() print(I) print(I.next()) print(I.next()) |
iteritems() returns an iterator object so you need to call the next method to get individual tuples
1 2 3 |
<dictionary-itemiterator object at 0x1088dfa48> (1, 'x') (2, 'y') |
- Example 3
1 2 3 4 5 |
d = {1: 'x', 2: 'y'} I = d.iteritems() for key, val in I: print(key, val) |
Since I is an iterator object, we can use a for loop against it to print individual tuples
1 2 |
1 x 2 y |
- Example 4
1 2 3 4 5 6 |
d = {1: 'x', 2: 'y'} I = d.iteritems() d[3] = 'z' for key, val in I: print(key, val) |
You cannot change the dictionary while iterating (recall that iteritems() returns an iterator object). The solution is to use viewitems()
1 |
RuntimeError: dictionary changed size during iteration |
- Example 5
1 2 3 4 5 6 7 |
d = {1: 'x', 2: 'y'} I = d.viewitems() print(I) d[3] = 'z' for key, val in I: print(key, val) |
viewitems() returns a view to the dictionary items so it will work if you change the dictionary
1 2 3 4 |
dict_items([(1, 'x'), (2, 'y')]) 1 x 2 y 3 z |
Python 3.x
- Example 1
1 2 3 4 5 6 7 |
d = {1: 'x', 2: 'y'} I = d.items() print(I) d[3] = 'z' for key, val in I: print(key, val) |
The returned I object is a view class to the dictionary items
1 2 3 4 5 |
dict_items([(1, 'x'), (2, 'y')]) <class 'dict_items'> 1 x 2 y 3 z |
- Example 2
1 2 |
d = {1: 'x', 2: 'y'} I = d.iteritems() |
This will generate an error
1 |
AttributeError: 'dict' object has no attribute 'iteritems' |
- Example 3
1 2 |
d = {1: 'x', 2: 'y'} I = d.viewitems() |
This will generate an error:
1 |
AttributeError: 'dict' object has no attribute 'viewitems' |
Summary
Python 2.x | Python 3.x |
---|---|
items() dictionary method returns a list of tuples | items() method does not return a list of tuples |
iteritems() dictionary method returns an iterator not the full list | iteritems() method is removed from Python 3.x |
The dictionary cannot be edited while using iteritems() | The dictionary can be edited while using items() |
viewitems() returns a dynamic view of the dictionary key value pairs. It enables traversing the dictionary while editing its content | items() returns a dynamic view of the dictionary key value pairs. It enables traversing the dictionary while editing its content |
Thanks for visiting. Please use the comments section below for feedback