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

Diagram of How to Fix AttributeError: 'dict' object has no attribute 'iteritems'

Diagram

AttributeError: ‘dict’ object has no attribute ‘iteritems’ error occurs when you try to “access the iteritems() method on a dictionary, but it was deprecated in Python 3.7 and removed in Python 3.10.”

To fix the AttributeError: ‘dict’ object has no attribute ‘iteritems’ error, use the “items()” method instead of the “iteritems()” method.

Reproduce the error

main_dict = {
  'key1': 'value1',
  'key2': 'value2'
}

for key, value in main_dict.iteritems():
   print(key, value)

Output

'dict' object has no attribute 'iteritems'

Here are the ways to fix the error:

For Python 3, use the “items()” method

main_dict = {
  'key1': 'value1',
  'key2': 'value2'
}

for key, value in main_dict.items():
  print(key, value)

Output

key1  value1
key2  value2

For Python 2, use the “iteritems()” method

main_dict = {
  'key1': 'value1',
  'key2': 'value2'
}

for key, value in main_dict.iteritems():
  print(key, value)

Output

key1  value1
key2  value2

The iteritems() method was a legacy used in older Python versions. Using the iteritems() method is no longer necessary, as the items() method can be used instead.

Replacing iteritems() with items() can fix the error and make your code compatible with Python 3.

That’s it!

Related posts

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

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

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

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

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

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

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

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.