Check if a key is in dictionary Python Example
Hello in this tutorial, we will understand the different ways to check if a key is in a dictionary data structure in the python programming language.
1. Introduction
A dict
is a valuable data structure in the python programming and it holds the key-value pair. To find the existing of a key in the dict
data structure we will explore the two in-built functions available in the python programming language i.e. if-in statement
and __contains__(key)
.
- if-in statement checks whether or not a given key exists in the
dict
data structure __contains__(key)
method is a replacement to thehas_key
method which was deprecated in python 3.0
1.1 Setting up Python
If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.
2. Check if a key is in dictionary Python Example
I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of their choice.
2.1 if-in statement and __contains__(key) function
Let us understand this with the help of a code snippet.
if-in statement and __contains__(key) function
# Tutorial - how-to-check-if-a-key-exists-in-a-python-dictionary # approach 1 def check_key(dictionary, key_to_lookup): if key_to_lookup in dictionary: print('Key = {} is present with value = {}'.format(key_to_lookup, dictionary[key_to_lookup])) else: print('Key = {} is not present in the dict'.format(key_to_lookup)) # approach 2 def contains_key(dictionary, key_to_lookup): if dictionary.__contains__(key_to_lookup): print('Key = {} is present with value = {}'.format(key_to_lookup, dictionary[key_to_lookup])) else: print('Key = {} is not present in the dict'.format(key_to_lookup)) def main(): cars = {'a': 'Mercedes', 'b': 'Audi', 'c': 'Bmw'} print('\n==== Checking key existence via if-in statement ====\n') check_key(cars, 'a') check_key(cars, 'x') print('\n==== Checking key existence via contains ====\n') contains_key(cars, 'c') contains_key(cars, 'y') # driver code. if __name__ == '__main__': main() print("\nDone")
If everything goes well the following output will be shown in the IDE console.
Console Output
==== Checking key existence via if-in statement ==== Key = a is present with value = Mercedes Key = x is not present in the dict ==== Checking key existence via contains ==== Key = c is present with value = Bmw Key = y is not present in the dict Done
That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!
3. Summary
In this tutorial, we learned:
- An introduction to an if-in statement and
__contains__(key)
method in the python programming language - Sample program to understand the if-in statement and
__contains__(key)
method
You can download the source code of this tutorial from the Downloads section.
4. Download the Project
This was an example of an if-in statement and __contains__(key)
method in the python programming language.
You can download the full source code of this example here: Check if a key is in dictionary Python Example