Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

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

Python Dictionary

In this article will be focusing on the 4 ways to Check if Key Exists in a Python Dictionary. A Python Dictionary is basically a data structure wherein the data items are stored in a key-value pair.


Technique 1: ‘in’ operator to Check if 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 input Python dictionary.

Python in operator basically checks if a particular element or value is contained in a particular sequence such as 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.\n") 
		 
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 key ‘Ruby’ is present in the dict or not.

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 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 the statement in the else portion.

Example:

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.\n") 
		 
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.\n") 
		 
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 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 ‘None‘.

Syntax:

dict.get(key, default=None)

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

Example 1:

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



if inp_dict.get('Python')!=None: 
		print("The key is present.\n") 
		 
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, else it returns false.

Syntax:

dict.has_keys()

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.\n") 
		 
else: 
		print("The key does not exist in the dictionary.") 


Conclusion

Thus, in this article, we have unveiled and understood the various techniques to check if key exists in a Python dictionary.

I recommend all the readers to go through the below post to know more about Python Dictionary in a detailed manner.


References

Leave a Reply

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