close

[Solved] : ” ‘dict’ object has no attribute ‘iteritems’ “

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error : ” ‘dict’ object has no attribute ‘iteritems’ “ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How : ” ‘dict’ object has no attribute ‘iteritems’ ” Error Occurs?

Today I get the following error : ” ‘dict’ object has no attribute ‘iteritems’ “ in python.

How To Solve : ” ‘dict’ object has no attribute ‘iteritems’ ” Error ?

  1. How To Solve : ” 'dict' object has no attribute 'iteritems' ” Error ?

    To Solve : ” 'dict' object has no attribute 'iteritems' ” Error iteritems() was removed in python3, so you can't use this method anymore.

  2. : ” 'dict' object has no attribute 'iteritems' “

    To Solve : ” 'dict' object has no attribute 'iteritems' ” Error iteritems() was removed in python3, so you can't use this method anymore.

Solution 1

As you are in python3 , use dict.items() instead of dict.iteritems()

iteritems() was removed in python3, so you can’t use this method anymore.

Take a look at Python 3.0 Wiki Built-in Changes section, where it is stated:

Removed dict.iteritems()dict.iterkeys(), and dict.itervalues().

Instead: use dict.items()dict.keys(), and dict.values() respectively.

Solution 2


In Python2, we had .items() and .iteritems() in dictionaries. dict.items() returned list of tuples in dictionary [(k1,v1),(k2,v2),...]. It copied all tuples in dictionary and created new list. If dictionary is very big, there is very big memory impact.

So they created dict.iteritems() in later versions of Python2. This returned iterator object. Whole dictionary was not copied so there is lesser memory consumption. People using Python2 are taught to use dict.iteritems() instead of .items() for efficiency as explained in following code.

import timeit

d = {i:i*2 for i in xrange(10000000)}  
start = timeit.default_timer()
for key,value in d.items():
    tmp = key + value #do something like print
t1 = timeit.default_timer() - start

start = timeit.default_timer()
for key,value in d.iteritems():
    tmp = key + value
t2 = timeit.default_timer() - start

Output:

Time with d.items(): 9.04773592949
Time with d.iteritems(): 2.17707300186

In Python3, they wanted to make it more efficient, so moved dictionary.iteritems() to dict.items(), and removed .iteritems() as it was no longer needed.

You have used dict.iteritems() in Python3 so it has failed. Try using dict.items() which has the same functionality as dict.iteritems() of Python2. This is a tiny bit migration issue from Python2 to Python3.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read