How To Solve “AttributeError: ‘Dict’ Object Has No Attribute ‘Iteritems'” In Python

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

This article will give you some solutions to fix “AttributeError: ‘dict’ object has no attribute ‘iteritems” in Python. Follow it and solve your problem.

What causes the “AttributeError: ‘dict’ object has no attribute ‘iteritems'” error?

Dictionary is a data type in Python. Dictionary is a list of elements with two fields: key and value. Each key-value pair can be considered an item. ‘Value’ can be any value. ‘Key’ must be an immutable data type such as string, number, or tuple when it passed to ‘item’.

AttributeError in Python is an error when you try to access a property or a method that does not exist for a particular object.

In Python 3, the method iteritems() has been aborted, thus resulting in the error “SyntaxError: Missing parentheses in call to ‘print'”.

Example:

myInfor = {
    'name': 'John', 
    'age': 18, 
    'job': 'Developer'
}

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

Output:

Traceback (most recent call last):
  File "code.py", line 7, in <module>
    for key, value in myInfor.iteritems():
AttributeError: 'dict' object has no attribute 'iteritems'

How to solve this error?

Using the ‘item()’ method

The item() method will return a new view of the dictionary(key, values).

Syntax:

dictionary.items()

Parameters: no parameters.

Example:

• Create a dictionary object. I named it myInfor.

• Use the item() function in conjunction with a for loop and print the key-value.

myInfor = {
    'name': 'John', 
    'age': 28, 
    'job': 'Developer'
}

for key, value in myInfor.items():
    print(key, ":", value)

Output:

name : John
age : 28
job : Developer

Using the ‘ dict.keys()’ method and  the ‘ dict.values()’ method

MethodsSyntaxParameters
The dict.key() method will return a new view of the dictionary key.dict.keys()No parameters.
The dict.values() method returns the value of the dictionary.dict.value()No parameters.

Example:

• Create a dictionary object. I named it myInfor.

• Use dict.value() and dict.key() function to print the ‘key’, ‘value’ of the myInfor dictionary.

myInfor = {
    'name': 'John', 
    'age': 28, 
    'job': 'Developer'
}

print(myInfor.keys())  
print(myInfor.values()) 

Output:

dict_keys(['name', 'age', 'job'])
dict_values(['John', 28, 'Developer'])

Note

To start debugging, you should see what properties the dictionary includes: Using print(dir(your_object))

Example:

myInfor = {
    'name': 'John', 
    'age': 28, 
    'job': 'Developer'
}

print(dir(myInfor))

Suppose you access any property that is not on this list. The program will report an “AttributeError: ‘dict’ object has no attribute ‘iteritems’ “error.

Summary

If you have any questions about the error “AttributeError: ‘dict’ object has no attribute ‘iteritems‘” in Python, leave a comment below. I will answer your questions.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *