Select Page

How to Solve AttributeError: ‘dict’ object has no attribute ‘iteritems’

by | Programming, Python, Tips

We use dictionaries to store data values in key-value pairs in Python. The dictionary method iteritems() returns an iterator of the dictionary‘s list as key-value tuple pairs.

As of Python major version 3, the items() method replaced iteritems(). If you try to call the iteritems() method on a dictionary using Python 3, you will raise the AttributeError: ‘dict’ object has no attribute ‘iteritems’.

If you are using Python 3, you can no longer use iteritems(). Use the items() method instead.

This tutorial will go through the error in detail and solve it with code examples.

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

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘dict’ object has no attribute ‘iteritems’” tells us that the dict object we are handling does not have the iteritems() attribute. We will raise this error when calling the iteritems() method on a dictionary using Python 3.


Let’s look at an example where we create a dictionary and use the iteritems() method to get an iterator object containing the key-value pairs. The dictionary will contain pizza names as keys and their prices as values.

a_dict = {'margherita':7.99, 'pepperoni':8.99, 'four cheese':10.99}
x = a_dict.iteritems()
print(f'Iteritems iterator object: {x}')
print(f'Iteritems iterator object as list: {list(x)}')
AttributeError                            Traceback (most recent call last)
<ipython-input-1-82819261a912> in <module>
      1 a_dict = {'margherita':7.99, 'pepperoni':8.99, 'four cheese':10.99}
----> 2 x = a_dict.iteritems()
      3 print(f'Iteritems iterator object: {x}')
      4 print(f'Iteritems iterator object as list: {list(x)}')
AttributeError: 'dict' object has no attribute 'iteritems'

The Python interpreter throws the error because we are using Python 3. The dictionary method iteritems() exists in Python 2. In Python 3 items() replaced iteritems().

Solution #1: Use dict.items()

We can use the items() method to return a view object containing the dictionary’s key-value pairs as tuples in a list. Let’s look at the revised code:

a_dict = {'margherita':7.99, 'pepperoni':8.99, 'four cheese':10.99}
x = a_dict.items()
print(f'Items view object {x}')
print(f'Items as a list: {list(x)}')
Items view object dict_items([('margherita', 7.99), ('pepperoni', 8.99), ('four cheese', 10.99)])
Items as a list: [('margherita', 7.99), ('pepperoni', 8.99), ('four cheese', 10.99)]

We see that the items method returns a dict_items view object, which we can convert to a list using the built-in list() method.

Solution #2: Change Python Major Version from 3 to 2

If you want to use an earlier Python major version, you can use Conda to create a virtual environment with a Python 2 interpreter. To create a virtual environment, use the following command:

conda create -n py27 python=2.7

Then activate the environment using:

conda activate py27

You should see “py27” next to the command line prompt in parenthesis.

Let’s check what Python version we are using the sys module

import sys
2.7.16 |Anaconda, Inc.| (default, Sep 24 2019, 16:55:38) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]

Then, we can use the iteritems() method. Note that we also have to change the print statements not to include parentheses. We use parentheses for print statements in Python 3.

a_dict = {'margherita':7.99, 'pepperoni':8.99, 'four cheese':10.99}
x = a_dict.iteritems()
print 'The iteritems iterator object: ', x
print 'The iteritems iterator object as a list: ', list(x)
The iteritems iterator object:  <dictionary-itemiterator object at 0x7fa906492470>
The iteritems iterator object as a list:  [('four cheese', 10.99), ('margherita', 7.99), ('pepperoni', 8.99)]

Note that the items() method in Python 3 returns a view object and the iteritems() method in Python 2 returns an item iterator object.


Congratulations on reading to the end of this tutorial! The AttributeError: ‘dict’ object has no attribute ‘iteritems’ occurs when you try to call the iteritems() method on a dictionary using Python major version 3. You can only use iteritems() in Python 2.

To solve this error, use the items() method when using Python 3. Otherwise, revert to Python 2 by creating a virtual environment.

For further reading on deprecated functions in Python 2, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!