Answer: You can use the ‘in’ keyword to check if a given key already exists in a dictionary in Python.
Dictionaries in Python are used to store data in the form of keys and values. You can perform various operations on this data by using the built-in methods provided by Python. In this blog, we’ll explore different methods to check if a given key already exists in the dictionary.
Table of contents:
Different Methods to Check if a Key already Exists in a Dictionary in Python
Method 1: Using ‘In’ operator in Python
One of the easiest methods to see if the key is already in the dictionary is to use the ‘in’ operator. If the key is in the dictionary, it returns True; if not, it returns False.
Example:
Output:
Explanation:
In the above code, we’ve searched for the key ‘X’. If it does contain the statement it will return the true statement and if not it will print the false statement.
Method 2: Using get() method in Python
The get() is an inbuilt method to return the list of available keys present in the dictionary. This method is implemented by using a key inside the get() and if it contains a true statement will be printed if not a false statement will be printed.
Example:
Output:
Explanation:
In the above code, we’ve searched for the key ‘X’ in the list of keys returned by the get method.
Method 3: Using Exceptional Handling in Python
Exception handling is a programming method to make the code run even after an unexpected error occurs. This handles the KeyError exception if it arises and attempts to get the key’s value.
Example:
Output:
Explanation:
In the above code, we have implemented Exceptional handling by using try and except keywords. If the key is present in the dictionary it will print ‘key exists’ and if it is not present it will show KeyError as the key doesn’t exist. In the above case, the first key ‘Z’ exists in the dictionary and hence the output ‘Key already exists’, but for the key ‘A’, the compiler throws an error which is handled by the except clause and the output is ‘Key doesn’t exist’.
Method 4: Using count() Function using Python
The count() is a built-in function that counts the number of items present in the list. To find if a key already exists, convert the keys of the dictionary into a list and count the number of occurrences of the specific keyword.
Example:
Output:
Explanation:
Counting the number of times the key occurs will help you to find if the key already exists.
Method 5: Using key() function using Python
Keys can be accessed in a dictionary by using the key() function. This will access all the keys in the dictionary. By using the key() function we can find the occurrence of the key in the dictionary.
Example:
Output:
keys() function is used to access the keys in the dictionary. With a conditional statement, we can find the occurrence of the key in the dictionary.
Method 6: Using items() function using Python
The items() function is used to access the keys and values while iterating. This would be used to access the keys in the dictionary and occurrences can be found using conditional statements.
Example:
Output:
Explanation:
The items() function will be used to access the data in the dictionary.
Method 7: Using has_key() function using Python
The has_key() function is a built-in Python function that will return the value true if the specified key is already present in the dictionary and return false if it’s not.
Note: This function was removed in Python version 3. It will work on previous versions of Python.
Example:
Output:
Explanation:
The has_key() will return true if it exists otherwise it will return false.
Method 8: Using setdefault() function using Python
Python has many built-in functions, one of such is setdefault() which will return the key-value pairs. It is used to assign value for the key which is then used to find the presence of it in the dictionary.
Example:
Output:
Explanation:
The key was assigned to the variable using the setdefault function.
Method 9: Using pop() function using Python
Pop() is used to take out an item in the stack data structure. It can be used inside an exceptional handling where it tries to pop the key to find if it contains the key it will return and if it does not it will throw an error.
Example:
Output:
Explanation:
The pop() function will pop out the key from the dictionary.
Conclusion
In Python, you can check if the given key already exists in the dictionary using various methods ranging from simple to advanced ones. Knowing these techniques enables you to select the one that best suits your requirements.