How To Resolve AttributeError: ‘dict’ Object Has No Attribute ‘has_key’ In Python

AttributeError ‘dict’ object has no attribute ‘has_key’ in Python

There are three ways: use the _contains_() function, use the in operator and dict.get() function to fix the “AttributeError: ‘dict’ object has no attribute ‘has_key'” error in Python. Follow the article to better understand.

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

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, and the ‘key’ when passed to ‘item’ must be an immutable data type such as string, number or tuple.

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

The “AttributeError: ‘dict’ object has no attribute ‘has_key'” error happens when you try to access property has_key, which as of Python 3 has been removed.

Example: 

myDict = {'name': 'John', 'age': 28, 'job': 'developer'}

print(myDict.has_key('name'))
print(myDict.has_key('job'))
print(myDict.has_key('age'))

Output:

Traceback (most recent call last):
  File "prog.py", line 2, in <module>
    print(myDict.has_key('name'))
AttributeError: 'dict' object has no attribute 'has_key'

How to solve this error?

Use the function _contains_() 

Recently updated Python has added a great function using the _contains_() function, which makes it easy to fix the “AttributeError: ‘dict’ object has no attribute ‘has_key'”.

The _contains_() function will return True if ‘key’ is present in the dictionary and False if ‘key’ does not appear in the dictionary.

Example:

  • Create a dictionary.
  • Use _contains_() to check if ‘key’ is present in the dictionary.
myDict = {'name': 'John', 'age': 28, 'job': 'developer'}

# Use _contains_ to check if 'key' is present in the dictionary
print(myDict.__contains__('age'))
print(myDict.__contains__('name'))
print(myDict.__contains__('job'))

Output:

True
True
True

Use the ‘in’ operator

You can use the in operator to check if ‘key’ is in the dictionary. This method also returns True if that ‘key’ appears in the dictionary and False if ‘key’ does not.

Example:

  • Create a dictionary.
  • Use the in operator to check if ‘key’ is present in the dictionary.
myDict = {'name': 'John', 'age': 18, 'job': 'developer'}

# Use the 'in' operator to check if 'key' is present in the dictionary
print('age' in myDict )
print('name' in myDict)
print('job' in myDict)

Output:

True
True
True

Use dict.get() function

You can use the dict.get() function to get an element from the dictionary by passing the name ‘key’ to the function.

Syntax:

dict.get(key)

Parameters:

  • key: key of the element you want to get.

The dict.get() function will return the value for which ‘key’ occurs in the dictionary and return ‘none’ if the stored ‘key’ does not appear.

Example:

  • Create a dictionary.
  • Use the dict.get() function passing ‘key’ to the function.
  • If that ‘key’ is present in the function, it will output the value of ‘key’.
myDict = {'name': 'John', 'age': 28, 'job': 'developer'}

# Use the dict.get() function passing 'key' to the function
print(myDict.get('age'))
print(myDict.get('name'))
print(myDict.get('job'))

Output:

28
John
developer

Summary

If have any questions about the error “AttributeError: ‘dict’ object has no attribute ‘has_key'” in Python, please leave a comment below. I will answer your questions. Thanks for reading!

Maybe you are interested in similar errors:

AttributeError: ‘list’ object has no attribute ‘items’ in Python
AttributeError: ‘list’ object has no attribute ‘split’
AttributeError: ‘str’ object has no attribute in Python
AttributeError: ‘tuple’ object has no attribute in Python
AttributeError: module ‘aiobotocore’ has no attribute ‘AioSession’
AttributeError: module ‘keras.engine’ has no attribute ‘Layer’ in Python
AttributeError: ‘dict’ object has no attribute ‘append’

Leave a Reply

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