A Python dictionary is a list of objects that are not in any particular order i.e Unordered.
A dictionary is made up of a collection of key-value pairs. Each key-value pair corresponds to a specific value.
Curly braces { } can be used to describe a dictionary by enclosing a comma-separated list of key-value pairs.
Every key is separated from its associated value by a colon “:”.
Given a dictionary, the task is to determine whether the given key exists in the dictionary.
Examples:
Input :
dictionary = {'This': 100, 'is':200, 'python':300} , key = is
Output:
Yes
Check whether given Key already exists in a Python Dictionary
There are several ways to check whether the given key exists in the dictionary some of them are:
- Using keys() function
- Using if-in Statements
- Using List and if statement
- Using Exceptional Handling
- Using get() function
- Using ‘if not in’ statement
Method #1: Using keys() function
The keys() method returns a list of all the available keys in the dictionary. Using the inbuilt method keys(), use an if statement and the ‘in’ operator to determine whether or not the key exists in the dictionary.
# Given Dictionary dictionary = {'this': 100, 'is': 200, 'python': 300} # Given key key = 'is' # Checking if key is exist in dictionary using keys() method if key in dictionary.keys(): # If the key exits then print yes print("Yes") # If the key do not exist then print no else: print("No")
Output:
Yes
Method #2: Using if-in Statements
To verify if a key exists in the dictionary, we can use the ‘in operator’ directly with the dictionary.
The expression:
key in dictionary
If the key exists in the dictionary, it will evaluate to True; otherwise, it will evaluate to False.
Let’s use this to see if the key is in the dictionary.
# Given Dictionary dictionary = {'this': 100, 'is': 200, 'python': 300} # Given key key = 'is' # Using if and in to check whether the key is present in dictionary if key in dictionary: # If the key exits then print yes print("Yes") # If the key do not exist then print no else: print("No")
Output:
Yes
Method #3: Using List and if statement
- Convert the dictionary keys to list.
- Check if the key present in this list using if statement.
Below is the implementation:
# Given Dictionary dictionary = {'this': 100, 'is': 200, 'python': 300} # Given key key = 'is' # Converting dictionary keys to list keyslist = list(dictionary.keys()) # Checking if key is exist in list using if if key in keyslist: # If the key exits then print yes print("Yes") # If the key do not exist then print no else: print("No")
Output:
Yes
Method #4: Using Exceptional Handling
It will raise a KeyError if we try to access a value of key that does not exist in the dictionary. This can also be used to see if anything exists in the dict.
This can also be a way to check if exist in dictionary or not .
Below is the implementation:
# Given Dictionary dictionary = {'this': 100, 'is': 200, 'python': 300} # Given key key = 'is' # Using try and except try: # If the key exist in dictionary its value is assigned to test and then it prints yes test = dictionary[key] print("Yes") # If the key do not exist in dictionary it prints no except KeyError: print("No")
Output:
Yes
Method #5: Using get() function
The dict class in Python has a get() method that accepts a key and a default value.
dictionary.get(keyname, value)
The function’s behaviour,
- If the given key is found in the dictionary, it returns the value associated with that key.
- If the given key does not exist in the dictionary, the passed default value argument is returned.
- If the given key does not exist in the dictionary and the default value is not provided, it returns None.
Below is the implementation:
# Given Dictionary dictionary = {'this': 100, 'is': 200, 'python': 300} # Given key key = 'is' # Using get() if dictionary.get(key) is not None: print("Yes") else: print("No")
Output:
Yes
Method #6 : Using ‘if not in’ statement
We’ve just verified whether the key exists in the dictionary so far.
However, if we want to verify if a key does not exist in the dictionary, we can use ‘if not in’ with the dictionary directly.
Below is the implementation:
# Given Dictionary dictionary = {'this': 100, 'is': 200, 'python': 300} # Given key key = 'is' # Using if not in if key not in dictionary: # if the key not in dictionary then we print no print("No") else: print("Yes")
Output:
Yes
Related Programs:
- how to check if a file or directory or link exists in python
- how to check if a given key exists in a map cpp
- python check if a value exists in the dictionary
- python how to check if an item exists in list
- how to convert a list to dictionary in python
- python how to sort a dictionary by key or value
- python how to get all keys with maximum value in a dictionary