Check multiple keys exist in Python Dictionary

Python Dictionary

In this post, we are going to learn about Check multiple keys exist in Python Dictionary or not, we access the values based on the key. So we have to first check whether the key exists in a dictionary or not. We are using these ways to Check all given keys that exist in a Python Dictionary.

1. All() to check given keys exist in Dictionary

The all() function in python takes an iterable as an argument and returns true if all elements of iterable (can be list, tuple, dictionary) are true else false.

Syntax

all(iterable)

Example code

In this code example, we are using if with all() to check if all the given keys exist in the dictionary. When it is returning true then the “given all keys are present in dictionary” message is showing on output else “given all “keys are not present in dictionary”

#Program to Check all keys exist in Dictionary in pytyon

dict_sub = {'math':100,'Eng':100,'Chem':98,'students':['jack','mack']}

 #checking all keys exist in dictionary

if all(key in dict_sub for key in ('math', 'Eng','Chem')): 
    print("given all keys are present in dictionary") 
else: 
    print("given all keys are not present in dictionary") 

Output

given all keys are present in dictionary

2. isSubset() to check multiple keys exist in Dictionary

The python isSubset() method returns true if all the elements of the set passed as argument present in another subset else return false. In the below code example we will use issubset() to check multiple key exist in Dictionary or not.

Syntax

set.issubset(set)
  • In this code example, we are converting all given keys that we have to find in the dictionary to set.
  • Using the issubset() method to check if all keys exist in the dictionary.
  • If it returns true “given all keys are present in the dictionary” will be shown up on output else “given all keys are not present in the dictionary”

Code Example

#Program to Check all keys exist in Dictionary in pytyon

dict_sub = {'math':100,'Eng':100,'Chem':98,'students':['jack','mack']}
 #checking all keys exist in dictionary

if set(('math', 'Eng','Chem')).issubset(dict_sub.keys()):

    print("given all keys are present in the dictionary") 
else: 
    print("given all keys are not present in  the dictionary") 
  

Output

given all keys are present in the dictionary

3. <= operator to Check multiple keys exist in Dictionary

In this example code, we are using a comparison operator to check if all keys exist in the dictionary. We are comparing dict_sub.keys() to the keys we have to all keys we have to find in the dictionary. If it returns true then the “given all keys are present” message will print in output else given all “keys are not present”

#Program to Check all keys exist in Dictionary in pytyon
dict_sub = {'math':100,'Eng':100,'Chem':98,'students':['jack','mack']}
given_keys = {'math', 'Eng','Chem'}
#checking all keys exist in dictionary
if dict_sub.keys() >= given_keys:

    print("given all keys are present in dictionary") 
else: 
    print("given all keys are not present in dictionary") 
  

Output

given all keys are present in dictionary

4. Using set to Check multiple keys exist in Dictionary

In this code example, we are comparing the given keys to all dictionary keys. If all keys are found in the dictionary it will return true else it returns false.

Code Example

#Program to Check all keys exist in Dictionary in pytyon
dict_sub = {'math':100,'Eng':100,'Chem':98,'students':['jack','mack']}
given_keys = {'math', 'Eng','Chem'}
#checking all keys exist in dictionary
if given_keys <= set(dict_sub):
 print('all given keys exist in dictionary')
else:
  print('all given keys does not exist in dictionary')

Output

all given keys exist in dictionary

Summary:

In this post, we have learned ways to Check multiple keys exist in Python Dictionary. In this post, we have used all() methods in python, a subset(), or a comparison operator. Now using these code example we can find all given keys exist in Dictionary or not.

Happy Learning!!