4 Easy Techniques to Check if Key Exists in a Python Dictionary

Check If Key Exists In A Dictionary

Checking whether a key exists in a Python dictionary is a common operation used in many scenarios. For instance, if you try to access a key value that doesn’t exist, you’ll get a KeyError. To avoid this you can check for the existence of the key. This way you not only handle this error but also prevent unexpected behavior in the code while performing any operation on the dictionary.

This tutorial will explore the four most commonly used ways to check if the key exists in a dictionary in Python. We’ll also look at the syntax for each method and demonstrate them with examples. so let’s get started!


Technique 1: ‘in’ operator to Check if a Key Exists in a Python Dictionary

Python in operator along with if statement can be used to check whether a particular key exists in the dictionary.

Python in operator is basically used to check for memberships. It can check if a particular element or value is contained in a particular sequence such as a list, tuple, dictionary, etc.

Syntax:

for/if value in iterable:

Example:

inp_dict = {'Python': "A", 'Java':"B", 'Ruby':"C", 'Kotlin':"D"} 

search_key = 'Ruby'

if search_key in inp_dict: 
		print("The key is present.") 
		 
else: 
		print("The key does not exist in the dictionary.") 

In the above example, we have used an if statement along with Python in operator to check whether the given key ‘Ruby’ is present in the dict or not. Additionally, you can also implement for loop if there are multiple keys to check.

Output:

The key is present.

Technique 2: Python keys() method

Python in-built keys() method can be used to check for the presence of a key in the existing dictionary.

Syntax:

dict.keys()

The keys() method takes no arguments and returns an object that represents a list of all the keys present in a particular input dictionary.

So, in order to check whether a particular key is present in the dict, we can use Python if statement along with the keys() method to compare the search_key with the list of keys returned from the keys() method. If the key is present, it will follow the statement in the if portion, otherwise, it will jump to the statement in the else portion.

Example 1:

inp_dict = {'Python': "A", 'Java':"B", 'Ruby':"C", 'Kotlin':"D"} 

search_key = 'Ruby'

if search_key in inp_dict.keys(): 
		print("The key is present.") 
		 
else: 
		print("The key does not exist in the dictionary.") 

Output:

The key is present.

Example 2:

inp_dict = {'Python': "A", 'Java':"B", 'Ruby':"C", 'Kotlin':"D"} 

search_key = 'Cpp'

if search_key in inp_dict.keys(): 
		print("The key is present.") 
		 
else: 
		print("The key does not exist in the dictionary.") 

Output:

The key does not exist in the dictionary.

Technique 3: get() method to Check if a Key Exists in a Python Dictionary

Python get() method can be used to check whether a particular key is present in the key-value pairs of the dictionary.

The get() method actually returns the value associated with the key if the key happens to be present in the dictionary, else it returns the default value ‘None‘.

Syntax:

dict.get(key, default=None)

We pass the key to be searched as an argument to the get() method, and if get() does not return None i.e. if the key is present in the dict, we print it.

Example:

inp_dict = {'Python': "A", 'Java':"B", 'Ruby':"C", 'Kotlin':"D"} 

if inp_dict.get('Python')!=None: 
		print("The key is present.") 
		 
else: 
		print("The key does not exist in the dictionary.") 

Output:

The key is present.

Technique 4: Python has_key() method

Note: The has_keys() method has been omitted from Python version 3 and above.

Python has_key() method checks whether a particular key is available in the dict and returns True if it is, else it returns false.

Syntax:

dict.has_keys(key)

Example:

inp_dict = {'Python': "A", 'Java':"B", 'Ruby':"C", 'Kotlin':"D"} 

search_key = 'Kotlin'

if inp_dict.has_key(search_key): 
		print("The key is present.") 
		 
else: 
		print("The key does not exist in the dictionary.") 

Output:

The key is present.

Conclusion

The dictionary is a data structure that contains data in key-value pairs. In this tutorial, using the Python program, we have seen multiple methods to check if a key exists in a dictionary.

We have checked this by using the 'in' operator, keys() method, get() method and has_key() method. Each one offers its own advantages and suitability for different scenarios.  With this knowledge, you can efficiently check whether a key exists in a dictionary or not. Hope you enjoyed reading the content.


Reference

https://stackoverflow.com/questions/1323410/should-i-use-has-key-or-in-on-python-dicts