While working with a dictionary object in Python 3, you might get the following error:
AttributeError: 'dict' object has no attribute 'iteritems'
This error occurs because the iteritems()
method from the dictionary
object was deprecated and removed in Python 3.
A new items()
method was introduced in Python 3, which has nice improvements over the iteritems()
method.
To fix this error, you need to replace the call to iteritems()
with items()
as shown below:
names = {1: "Nathan", 2: "May", 3: "John"}
iterator = names.items() # ✅
for i in iterator:
print(i)
Output:
dict_items([(1, 'Nathan'), (2, 'May'), (3, 'John')])
(1, 'Nathan')
(2, 'May')
(3, 'John')
The following section will explain why iteritems()
was removed, and show what improvements were introduced in the items()
method.
The items() method was improved in Python 3
In Python 2, the dictionary
object already has the items()
method, but this method returns a copy of the dictionary’s items as a (key, value)
tuple.
Here’s an example:
names = {1: "Nathan", 2: "May", 3: "John"}
iterator = names.items()
print(iterator)
for i in iterator:
print(i)
Output:
[(1, 'Nathan'), (2, 'May'), (3, 'John')]
(1, 'Nathan')
(2, 'May')
(3, 'John')
Returning a copy of the dictionary’s items consumed a lot of memory, so a new method was added to the dictionary
object to solve this issue.
The iteritems()
method was an improved version of the items()
method. It returns an iterator object that points to the dictionary items in the memory, removing the need to create a copy.
You can see this by printing the object returned by iteritems()
as follows:
names = {1: "Nathan", 2: "May", 3: "John"}
iterator = names.iteritems()
print(iterator)
# <dictionary-itemiterator object at 0x7faac012aa10>
As you can see, the print()
function returns <dictionary-itemiterator>
object. This iterator still prints a tuple when you use it in a for
loop.
But the iterator object has two notable weaknesses:
- You can iterate only once
- Adding or deleting entries in the dictionary may raise a
RuntimeError
Suppose you delete an item from the dictionary after calling the iteritems()
method like this:
names = {1: "Nathan", 2: "May", 3: "John"}
iterator = names.iteritems()
del names[1]
for i in iterator:
print(i)
You’ll get this error:
Traceback (most recent call last):
File "main.py", line 6, in <module>
for i in iterator:
RuntimeError: dictionary changed size during iteration
When developers of Python 3 fixed these two problems and removed the original items()
method.
The items()
method in Python 3 returns a view object, giving you an optimized and dynamic access to the items in the dictionary.
This means you can use the iterator as many times as you need. You can even delete some entries and the view will adjust:
names = {1: "Nathan", 2: "May", 3: "John"}
iterator = names.items()
del names[1]
for i in iterator:
print(i)
Output in Python 3:
(2, 'May')
(3, 'John')
(4, 'Anna')
And that’s why the iteritems()
was removed in Python 3.
To make your code compatible with Python 3, you need to replace all occurrences of iteritems()
with items()
.
If you used iteritems()
extensively, use a Text Editor like VSCode or Sublime Text to help you replace all occurrences of the method.
You can also use the Python 2to3 tool, but I found that it adds an unnecessary call to the iter()
function as follows:
-iterator = names.iteritems()
+iterator = iter(names.items())
I prefer to refactor the code manually, but of course you can use 2to3
if you want. 😉
Thanks for reading this far! I hope this article is helpful. See you later. 👋