In this Python tutorial, we will learn how to check if a Key Exists in a Python Dictionary. To understand various approaches, we’ll use some built-in functions to check if a Key Exists in Python Dictionary.
As a Developer, while making the Python Project I got the requirement to check whether the key is available in a dictionary or not.
Here we will see:
- How to check if a Key Exists in a Python Dictionary using in operator
- Check if a Key Exists in a Python Dictionary using get()
- How to check if a Key Exists in a Python Dictionary using keys()
- Check if a Key Exists in a Python Dictionary using has_key()
- How to check if a Key Exists in a Python Dictionary using count()
Check if a Key Exists in a Python Dictionary
In Python, there are numerous methods for checking if a key exists in a Python Dictionary. We’ll go through how to Check if a Key Exists in Python Dictionary specifically utilizing the ‘in’ operator, and keys() method.
How to check if a Key Exists in a Python Dictionary using in operator
- In this section, we will discuss how to check if a Key Exists in Dictionary using the ‘in’ operator in Python.
- We can easily use the Python in operator and the if statement to determine whether a specific key is present in the input Python dictionary.
- In this example, we will create a dictionary and set the condition of whether the key is available in the dictionary or not.
Syntax:
Here is the syntax of the ‘in’ operator in Python
if value in iterable:
Example:
Let’s take an example and check how to check if a Key Exists in Dictionary using the ‘in’ operator in Python.
Source code:
Country_name = {"U.S.A": 567, "Germany":945, "Australia":189}
if "U.S.A" in Country_name:
print("Key exists in Dictionary")
else:
print("Key does not exist in Dictionary")
To determine whether the key “U.S.A.” is present in the dict or not, we used an if statement and the Python in operator
Here is the Screenshot of the following given code.
This is how to check if a Key Exists in a Python Dictionary using in operator.
Read: Python Dictionary sort
Check if a Key Exists in a Python Dictionary using get()
- Now let us discuss how to Check if a Key Exists in a Python Dictionary using get().
- If the key is found in the dictionary, the get() method really returns the value associated with it; otherwise, it returns ‘None’.
- Using this method we can easily assign a key and check if a key exists in the python dictionary.
Syntax:
Let’s have a look at the Syntax and understand the working of the get() method in Python
dict.get(key, default=None)
- It consists of a few parameters
- key: This parameter defines the key that we want to be searched.
- default: A value to return if the given key is not found. By default the value is None.
Example:
Let’s take an example and check if a Key Exists in a Python Dictionary using get().
Source Code:
Cars_in_USA = {'Tesla': 656, 'BMW':9345, 'Mercedes':897}
if Cars_in_USA .get('Tesla')!=None:
print("The key is exist in dictionary")
else:
print("The key not present in the dictionary.")
In the following given code, we used the dict.get() method in which we assign the key element and it will check whether it contains in the dictionary or not.
You can refer to the below Screenshot
As you can see in the Screenshot we have discussed how to Check if a Key Exists in a Python Dictionary using get().
Read: Python Dictionary index
How to check if a Key Exists in a Python Dictionary using keys()
- In this section, we will discuss how to check if a Key Exists in a Python Dictionary using keys().
- To extract all the keys from the dictionary, use the keys() function in Python. If the dictionary is empty, it produces an empty list. There is no parameter required for this procedure.
- Therefore, to determine whether a specific key is present in the dict, we compare the search key with the list of keys returned by the keys() method using the if statement in Python.
Syntax:
Let’s have a look at the Syntax and understand the working of dict.keys() method
dict.keys()
Note: This method does not take any argument and it will return the list of keys.
Example:
Let’s take an example and check if a Key Exists in a Python Dictionary using keys().
Source code:
cities_in_USA = {'New York': 723, 'Los Angeles':445, 'California':924, 'Los Angeles':893}
check_key = 'Los Angeles'
if check_key in cities_in_USA.keys():
print("The key is exist in dictionary")
else:
print("The key not present in the dictionary.")
To determine whether the key “Los Angeles” is present in the dict or not, we used a dict.keys() method.
Here is the implementation of the following given code.
In this example, we have understood how to check if a Key Exists in a Python Dictionary using keys.
Read: Python Dictionary Count
Check if a Key Exists in a Python Dictionary using has_key()
- In this section, we will discuss how to check if a Key Exists in a Python Dictionary using has_key().
- If a specified key is present in the dictionary, the method has a key() that returns true; otherwise, it returns false.
- Use the if statement to determine whether the key is present in the dictionary using the built-in method has_key(). The has_key() function is no longer present in Python 3. It is therefore limited to Python 2 use.
Syntax:
Here is the Syntax of has_key() method in Python
dict.has_keys()
Example:
Country_name = {"U.S.A": 567, "Germany":945, "Australia":189}
if Country_name.has_key('Germany'):
print("Key exists in Dictionary")
else:
print("Key does not exist in Dictionary")
You can refer to the below screenshot
The reason for this error is the has_key() function has been removed in Python 3 version. To solve this problem we have to install the python 2.7 version.
Read: Python dictionary initialize
How to check if a Key Exists in a Python Dictionary using count()
- Now let us see How to check if a Key Exists in a Python Dictionary using count().
- The count() method can be used to determine whether a key is present in the dictionary; if the key’s count is 1, the key is present; otherwise, it is not.
- The count() function in Python returns how many times an element appears in a list. It returns 0 if the element is absent from the list.
Example:
Here we will take an example and check if a Key Exists in a Python Dictionary using count().
Source Code:
bikes_in_USA = {'BMW': 893, 'KAWASKI': 627, 'Harley-Davidson': 734}
new_val = list(bikes_in_USA.keys())
new_result = "It is not available"
if(new_val.count('KAWASKI') == 1):
new_result = "It is available"
print(new_result)
In the above code first, we created the dictionary named bikes_in_USA and then used it the convert it into the list. Next, we set the condition of whether the given key is available in the dictionary or not.
Here is the Screenshot of the following given code
Also, check the following tutorial related to Python Dictionary.
- Python dictionary contains
- Python Dictionary update
- Python dictionary of lists
- Python Dictionary Copy
In this article, we have discussed how to check if a Key Exists in Python Dictionary, and also we have covered the following topics.
- How to check if a Key Exists in a Python Dictionary using in operator
- Check if a Key Exists in a Python Dictionary using get()
- How to check if a Key Exists in a Python Dictionary using keys()
- Check if a Key Exists in a Python Dictionary using has_key()
- How to check if a Key Exists in a Python Dictionary using count()
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.