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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

AttributeError: 'dict' object has no attribute 'has_key' #

The "AttributeError: 'dict' object has no attribute 'has_key'" occurs because the has_key() method has been removed in Python 3. To solve the error, use the in operator to check if a key is in a dictionary, e.g. if 'age' in my_dict.

attributeerror dict object has no attribute has key

Here is an example of how the error occurs.

main.py
my_dict = {'name': 'Alice', 'age': 30} # ⛔️ AttributeError: 'dict' object has no attribute 'has_key' if my_dict.has_key('age'): print('Key in dict')

The has_key method has been removed in Python 3, however we can use the in operator to check if a key is in a dictionary.

main.py
my_dict = {'name': 'Alice', 'age': 30} # ✅ Check if key is in dictionary if 'age' in my_dict: print('age key is in dictionary') print(my_dict['age']) # 👉️ 30 # ✅ Check if key is NOT in dictionary if 'some_key' not in my_dict: print('key is NOT in dictionary')

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

x not in s returns the negation of x in s.

All built-in sequences and set types support the in and not in operators.

When used with a dictionary, the in operators check for the existence of the specified key in the dict object.

You can use the get() method to access a key in a dictionary without getting a KeyError if the key is not present.

main.py
my_dict = {'name': 'Alice', 'age': 30} print(my_dict.get('name')) # 👉️ "Alice" print(my_dict.get('another')) # 👉️ None

The dict.get method returns the value for the given key if the key is in the dictionary, otherwise a default value is returned.

The method takes the following 2 parameters:

NameDescription
keyThe key for which to return the value
defaultThe default value to be returned if the provided key is not present in the dictionary (optional)

If a value for the default parameter is not provided, it defaults to None, so the get() method never raises a KeyError.

A good way to start debugging is to print(dir(your_object)) and see what attributes a dictionary has.

Here is an example of what printing the attributes of a dict looks like.

main.py
my_dict = {'name': 'Alice', 'age': 30} # [...'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', # 'pop', 'popitem', 'setdefault', 'update', 'values' ...] print(dir(my_dict))

If you pass a class to the dir() function, it returns a list of names of the class's attributes, and recursively of the attributes of its bases.

If you try to access any attribute that is not in this list, you would get the "AttributeError: 'dict' object has no attribute error".

Since dict objects don't have a has_key() method, the error is caused.

Conclusion #

The "AttributeError: 'dict' object has no attribute 'has_key'" occurs because the has_key() method has been removed in Python 3. To solve the error, use the in operator to check if a key is in a dictionary, e.g. if 'age' in my_dict.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.