Solving AttributeError: ‘dict’ object has no attribute ‘iteritems’

AttributeError: ‘dict’ object has no attribute ‘iteritems’ is an error which occurs in Python when a developer uses iteritems() in Python 3 which should not be done.

In this article we are going to explain why the error is popping up and show you how to solve the error and get rid of it for good.

Describing AttributeError: ‘dict’ object has no attribute ‘iteritems’

In Python 3 a lot of methods that were in Python before got removed, one of those methods is the iteritems() method.

Before we solve this error, let us try to replicate a scenario where this takes place.

This error will occur in any script that uses the removed method iteritems(), here is an example of the error message.

                                                                       #
Traceback (most recent call last):
f[78].iteritems(): AttributeError: 'dict' object has no attribute 'iteritems'
                                                                       #

In the sections bellow we will explain the root of the error more and propose some possible fixes.

Solution : Using dict.items()

We explained before why we can not use iteritems() in our Python 3 code, the method has been simply removed, so, what is the alternative?

Instead of doing

                                                                       #
f[78].iteritems() 
                                                                       #

Instead, we should use .

                                                                       #
f[78].items()
                                                                       #

This error could be confusing at first. But once you understand why it is happening, it is easy to solve by only using a more recent method that works and achieves the same function.

If you insist on using the method iteritems() the only thing you can do in that case is to downgrade from Python 3 to Python 2 which is not the best idea in my opinion.

Summing-up

I hope you guys found my article helpful, I hope the solution provided above has solved your problem.

If you like the effort I put in this post please consider donating to our Kofi account using the red button at

the top of this page. Keep learning guys, keep coding and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/