DevOps | Cloud | Analytics | Open Source | Programming





How To Fix Python Error - dict object has no attribute iteritems ?



In this post , we will see How To Fix Python Error: “ 'dict' object has no attribute 'iteritems' ”.


AttributeError: 'dict' object has no attribute 'iteritems'

This is migration issue of method using for Python 2 to Python 3. Read below to understand the concept.

For Python 3:

Python 3 had made slight changes to the usage of iteritems() & items(). iteritems() gives an iterator to operate of the data .But Python 3 does not support such method. And hence slight modifications are done as given below  - In Python 3 ,


Old usage --> dict.iteritems()   --> returns list

NEW (Use this) ---> dict.items() ---> returns view

 


Old usage --> dict.iterkeys() --> returns list

NEW (Use this) ---> dict.keys() ---> returns view

 


Old usage --> dict.itervalues() --> returns list

NEW (Use this) ---> dict.values() ---> returns view

 

For Python 2:

Python 2 supported both the methods for dictionaries -  items() and iteritems() . items() --> This method returns a list of tuples in the dictionary [(key1,val1),(key2,val2) etc.]. Basically it copies all tuples in dictionary and creates a new list. So if you have very Big dictionary , this process is not memory efficient. iteritems()  ---> This method was introduced in subsequent versions of Python 2. This method returns an iterator object instead of copying an entire dictionary (unlike items() method). Thereby promoting better memory efficiency.  And this habit was passed on to Python 3 for the obvious reason of retaining Best Practice.   Hope this helps .  

**Other Interesting Reads - **

  dict object' has no attribute 'iteritems ansible ,dict' object has no attribute python3 ,'dict' object has no attribute 'has_key' ,attributeerror dict object has no attribute session ,'dict_values' object has no attribute 'next' ,attributeerror: 'dict' object has no attribute 'read ,dict' object has no attribute sqrt ,dict' object has no attribute 'iterkeys ,dict' object has no attribute 'iteritems' ,dict' object has no attribute 'itervalues' ,dict' object has no attribute 'iterkeys' ,dict' object has no attribute 'iteritems' python3 ,dict' object has no attribute 'item' ,dict' object has no attribute 'iterkeys' python 3 ,dict' object has no attribute 'iter' ,dict object' has no attribute 'iteritems' ansible ,dict' object has no attribute 'itemgetter' ,dict_values' object has no attribute 'items' ,dict object' has no attribute ansible ,dict' object has no attribute python ,dict' object has no attribute python3 ,dict object' has no attribute 'iteritems ansible ,dict' object has no attribute 'iterkeys