Python | Check if given multiple keys exist in a dictionary
A dictionary in Python consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value.
Input : dict[] = {“geeksforgeeks” : 1, “practice” : 2, “contribute” :3}
keys[] = {“geeksforgeeks”, “practice”}
Output : YesInput : dict[] = {“geeksforgeeks” : 1, “practice” : 2, “contribute” :3}
keys[] = {“geeksforgeeks”, “ide”}
Output : No
Let’s discuss various ways of checking multiple keys in a dictionary :
Method #1 Using comparison operator :
This is the common method where we make a set which contain keys that use to compare and using comparison operator we check if that key present in our dictionary or not.
# Python3 code to check multiple key existence # using comparison operator # initializing a dictionary sports = { "geeksforgeeks" : 1 , "practice" : 2 , "contribute" : 3 } # using comparison operator print (sports.keys() > = { "geeksforgeeks" , "practice" }) print (sports.keys() > = { "contribute" , "ide" }) |
True False
Method #3 Using issubset() :
In this method, we will check the keys that we have to compare is subset() of keys in our dictionary or not.
# Python3 code heck multiple key existence # using issubset # initializing a dictionary sports = { "geeksforgeeks" : 1 , "practice" : 2 , "contribute" : 3 } # creating set of keys that we want to compare s1 = set ([ 'geeksforgeeks' , 'practice' ]) s2 = set ([ 'geeksforgeeks' , 'ide' ]) print (s1.issubset(sports.keys())) print (s2.issubset(sports.keys())) |
True False
Method #4 Using if and all statement :
In this method we will check that if all the key elements that we want to compare are present in our dictionary or not .
# Python3 code check multiple key existence # using if and all # initializing a dictionary sports = { "geeksforgeeks" : 1 , "practice" : 2 , "contribute" : 3 } # using if, all statement if all (key in sports for key in ( 'geeksforgeeks' , 'practice' )): print ( "keys are present" ) else : print ( "keys are not present" ) # using if, all statement if all (key in sports for key in ( 'geeksforgeeks' , 'ide' )): print ( "keys are present" ) else : print ( "keys are not present" ) |
keys are present keys are not present
Recommended Posts:
- Python dictionary with keys having multiple inputs
- Python | Add new keys to a dictionary
- Python Dictionary | keys() method
- Python | Get dictionary keys as a list
- Python | Initialize a dictionary with only keys from a list
- Python | Remove spaces from dictionary keys
- Python program to Swap Keys and Values in Dictionary
- Python | Check whether given key already exists in a dictionary
- Python dictionary, set and counter to check if frequencies can become same
- Python Dictionary | Check if binary representations of two numbers are anagram
- Combining multiple columns in Pandas groupby with dictionary
- Python | Pandas Series.keys()
- Python | Difference in keys of two dictionaries
- Python | Pandas dataframe.keys()
- Handling missing keys in Python dictionaries
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.