To get the idea how to do that we first inspect what methods we can call on dictionary. Here are the methods:
The brutal method to check if the key already exists may be the
The other two interesting methods
Printing shows the key we don't have will return
We use that to get the information if the key is present or no. But consider this if we create a dict with a single
Leading that This story should have a happier ending. If we use the
We get the correct results. We may examine the Python byte code:
This shows that Given a dictionary in Python our task is to check whether the given key is already present in the dictionary or not. If present, print “Present” and the value of the key. Otherwise, print “Not present”. Example Input : {'a': 100, 'b':200, 'c':300}, key = b Output : Present, value = 200 Input : {'x': 25, 'y':18, 'z':45}, key = w Output : Not present There can be different ways for checking if the key already exists, we have covered the following approaches:
Check If Key Exists using the Inbuilt method keys()Using the Inbuilt method keys() method returns a list of all the available keys in the dictionary. With the Inbuilt method keys(), use if statement with ‘in’ operator to check if the key is present in the dictionary or not. Python3
Output: Present, value = 200 Not present Check If Key Exists using if and inThis method uses the if statement to check whether the given key exists in the dictionary. Python3
Output: Present, value = 200 Not present Check If Key Exists using has_key() methodUsing has_key() method returns true if a given key is available in the dictionary, otherwise, it returns a false. With the Inbuilt method has_key(), use the if statement to check if the key is present in the dictionary or not. Note – has_keys() method has been removed from the Python3 version. Therefore, it can be used in Python2 only. Python
Output: Present, value = 200 Not present Check If Key Exists using get()Using the Inbuilt method get() method returns a list of available keys in the dictionary. With the Inbuilt method keys(), use the if statement to check if the key is present in the dictionary or not. If the key is present it will print “Present” Otherwise it will print “Not Present”. Python3
Output: Present Handling ‘KeyError’ ExceptionUse try and except to handle the KeyError exception to determine if a key is present in a dict. The KeyError exception is generated if the key you’re attempting to access is not present in the dictionary. Python3
Output: Example 1 The key doesn't exist in the dictionary Example 2 The key exists in the dictionary How do you check if a key exists in a dictionary Python?Check If Key Exists using has_key() method
Using has_key() method returns true if a given key is available in the dictionary, otherwise, it returns a false. With the Inbuilt method has_key(), use the if statement to check if the key is present in the dictionary or not.
How do you check if a key value pair exists in a dictionary Python?To check if a key-value pair exists in a dictionary, i.e., if a dictionary has/contains a pair, use the in operator and the items() method. Specify a tuple (key, value) . Use not in to check if a pair does not exist in a dictionary.
Which operator tests to see if a key exists in a dictionary?The simplest way to check if a key exists in a dictionary is to use the in operator. It's a special operator used to evaluate the membership of a value. This is the intended and preferred approach by most developers.
What is the keyword that is used to check if a particular key exists in a dictionary?The “in” Keyword
When querying a dictionary, the keys, and not the values, are searched.
|