Select Page

How to Solve Python AttributeError: ‘dict’ object has no attribute ‘has_key’

by | Programming, Python, Tips

We use dictionaries to store data values in key:value pairs in Python. The dictionary method has_key() returns True if a specified key is present in the dictionary. Otherwise, it returns False.

As of Python major version 3, has_key was removed from the dictionary methods. If you try to call the has_key() method on a dictionary using Python 3, you will raise the AttributeError: ‘dict’ object has no attribute ‘has_key’.

If you are using Python 3, you can no longer use has_key. Use the in operator instead.

This tutorial will go through the error in detail and how to solve it with code examples.


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

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘dict’ object has no attribute ‘has_key’” tells us that the dict object we are handling does not have the has_key attribute. We will raise this error when calling the has_key() method of a dictionary using Python 3.

Example

Let’s look at an example where we create a dictionary and use the has_key() method to search for a specific key. The dictionary will contain pizza names as keys and their prices as values.

a_dict = {'margherita':7.99, 'pepperoni':8.99, 'ham and pineapple':10.99}
print(a_dict.has_key('margherita')
print(a_dict.has_key('four cheeses')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-09e10e5c5c36> in <module>
      1 a_dict = {'margherita':7.99, 'pepperoni':8.99, 'ham and pineapple':10.99}
----> 2 print(a_dict.has_key('margherita'))
      3 print(a_dict.has_key('four cheeses'))
      4 

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

The Python interpreter throws the error because we are using Python 3. The dictionary method has_key() exists in Python 2.

Solution #1: Use in operator

We can use the in operator to check if a key is present in a dictionary to solve this error. Let’s look at the revised code:

a_dict = {'margherita':7.99, 'pepperoni':8.99, 'ham and pineapple':10.99}
print('margherita' in a_dict)
print('four cheeses' in a_dict)
True
False

The Margherita pizza is present in the dictionary, so the in operator returns True. The operator returns False for the four cheeses pizza, which is not in the dictionary.

Solution #2: Change Python Major Version from 3 to 2

If you want to use an earlier Python major version, you can use Conda to create a virtual environment with a Python 2 interpreter. To create a virtual environment, use the following command:

conda create -n py27 python=2.7

Then activate the environment using:

conda activate py27

You should see “py27” next to the command line prompt in parenthesis. Then you can use has_key() with no AttributeError:

conda activate py27

Let’s check what Python version we are using the sys module

import sys

print(sys.version)
2.7.16 |Anaconda, Inc.| (default, Sep 24 2019, 16:55:38) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]

Then use the has_key() method:

a_dict = {'margherita':7.99, 'pepperoni':8.99, 'ham and pineapple':10.99} }

print(a_dict.has_key('margherita'))

print(a_dict.has_key('four cheeses'))
True
False

Summary

Congratulations on reading to the end of this tutorial! The AttributeError: ‘dict’ object has no attribute ‘has_key’ occurs when you try to call the has_key() method on a dictionary using Python major version 3. You can only use has_key() in Python 2.

To solve this error, use the in operator to check if a key exists in a dictionary. Otherwise, revert to Python 2 by creating a virtual environment.

For further reading on AttributeErrors involving the list object, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!