Error: " 'dict' object has no attribute 'iteritems' " Error: " 'dict' object has no attribute 'iteritems' " python python

Error: " 'dict' object has no attribute 'iteritems' "


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.


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 timeitd = {i:i*2 for i in xrange(10000000)}  start = timeit.default_timer()for key,value in d.items():    tmp = key + value #do something like printt1 = timeit.default_timer() - startstart = timeit.default_timer()for key,value in d.iteritems():    tmp = key + valuet2 = timeit.default_timer() - start

Output:

Time with d.items(): 9.04773592949Time 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.


I had a similar problem (using 3.5) and lost 1/2 a day to it but here is a something that works - I am retired and just learning Python so I can help my grandson (12) with it.

mydict2={'Atlanta':78,'Macon':85,'Savannah':72}maxval=(max(mydict2.values()))print(maxval)mykey=[key for key,value in mydict2.items()if value==maxval][0]print(mykey)YEILDS; 85Macon