dict object has no attribute has_key ( Solution )

[ad_1]

Attributeerror: dict object has no attribute has_key  error occurs because has_key() function is deprecated syntax in python 3. But do not worry, There are multiple better alternatives in python v bucks generator 3 for this. In this article, we will explore them in more detail with practical syntax.

Attributeerror: dict object has no attribute has_key (Fix) –

As we mentioned, There are multiple ways to fix this error. We will address these solutions in different chunks. But before that, we will replicate the same error. For that, we will create a dummy simple python dict, and then we will use the has_key() function with the current python env 3. Here is the code and running screenshot for more clarity.

Root Cause & Error Replication –

sample_dict={'key1':'value1','key2':'value2'}
if sample_dict.has_key('key2'):
 print('Required key is avaiable')
Attributeerror dict object has no attribute has_key error replication
Attributeerror dict object has no attribute has_key error replication

Solution 1: Using in operator –

This solution is coming from the official release of python documentation. Let’s see the implementation here.

sample_dict ={'key1':'value1', 'key2':'value2'}
if "key2" in sample_dict:
 print('Required key is avaiable')
in operator alternative has_key()
in operator alternative has_key()

Reference :

has_key() depreciation
has_key() depreciation

Solution 2 : Using __contains__(key)  function –

__contains()__ function checks that key is available or not in the dict.  It is completely identical to the has_keys() function. Let’s see the implementation for a better understanding.

sample_dict = {'key1':'value1','key2':'value2'}
if(sample_dict.__contains__("key2")):
print('Required key is avaiable')
Attributeerror dict object has no attribute has_key solution2
Attributeerror dict object has no attribute has_key solution2

Solution 3: Downgrading python 3. x to python 2. x ( Not Recommended )-

We can downgrade the current python version to 2. x if it is 3. x. Since you already know that the depreciation will be in python 3.x only. But this is not the recommended way because when we downgrade v bucks generator the existing python it can create incompatibility with other modules. So be careful with it.

Solution 4: Custom logic of linear search –

It may be a bit time complex because here we have to iterate the dict keys and search the token key in the complete list of dict keys. Just like linear search. Since it is not the optimized way but it will anyways solve the problem. Hence you can also give it a try. Please refer to the below implementation for the same.

sample_dict = {'key1':'value1','key2':'value2'}
dict_keys_list=sample_dict.keys()
for i in dict_keys_list:
  if(i=="key2"):
   print('Required key is avaiable')
   break
has_keys alternative in python dict
has_keys alternative in python dict

Thanks
Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

[ad_2]

Source link