Check if multiple Keys exist in a Dictionary in Python

avatar
Borislav Hadzhiev

Last updated: Feb 20, 2023
4 min

banner

# Table of Contents

  1. Check if multiple Keys exist in a Dictionary in Python
  2. Check if multiple Keys are in a Dictionary using a set object
  3. Check if multiple Keys are in a Dictionary using a set.issubset() object
  4. Check if multiple Keys exist in a Dictionary using a for loop
  5. Check if multiple Keys exist in a Dictionary using and operator

# Check if multiple Keys exist in a Dictionary in Python

To check if multiple keys are in a dictionary:

  1. Use a generator expression to iterate over a tuple containing the keys.
  2. Use the in operator to check if each key is in the dictionary.
  3. Pass the result to the all() function.
main.py
my_dict = { 'name': 'Bobby Hadz', 'country': 'Austria', 'age': 30 } if all(key in my_dict for key in ("name", "country")): # ๐Ÿ‘‡๏ธ this runs print('multiple keys are in the dictionary')

check if multiple keys exist in dictionary

We wrapped the multiple keys we wanted to test for in a tuple and used a generator expression to iterate over the tuple.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the in operator to check if the current key is present in the dictionary.

The in operator tests for membership. For example, k in d evaluates to True if k is a member of d, otherwise, it evaluates to False.

When used with a dictionary, the operators check for the existence of the specified key in the dict object.
main.py
my_dict = { 'name': 'Bobby Hadz', 'country': 'Austria', 'age': 30 } print('name' in my_dict) # ๐Ÿ‘‰๏ธ True print('another' in my_dict) # ๐Ÿ‘‰๏ธ False

The last step is to pass the generator object to the all() function.

main.py
my_dict = { 'name': 'Bobby Hadz', 'country': 'Austria', 'age': 30 } if all(key in my_dict for key in ("name", "country")): # ๐Ÿ‘‡๏ธ this runs print('multiple keys are in the dictionary')

The all() built-in function takes an iterable as an argument and returns True if all elements of the iterable are truthy (or the iterable is empty).

If all of the keys are present in the dictionary, the all() function will return True, otherwise, False is returned.

main.py
my_dict = { 'name': 'Bobby Hadz', 'country': 'Austria', 'age': 30 } # ๐Ÿ‘‡๏ธ True print(all(key in my_dict for key in ('name', 'country'))) # ๐Ÿ‘‡๏ธ False print(all(key in my_dict for key in ('another', 'country')))

Alternatively, you can use a set object.

# Check if multiple Keys are in a Dictionary using a set object

This is a three-step process:

  1. Wrap the keys in a set object.
  2. Use the dict.keys() method to get a view of the dictionary's keys.
  3. Check if the multiple keys are present in the view of the dictionary's keys.
main.py
my_dict = { 'name': 'Bobby Hadz', 'country': 'Austria', 'age': 30 } if {'name', 'country'} <= my_dict.keys(): # ๐Ÿ‘‡๏ธ this runs print('multiple keys are in the dictionary')

check if multiple keys are in dictionary using set object

We used curly braces to add the keys as elements to a set object.

Set objects are an unordered collection of unique elements

The benefit of using a set is that we can check if the elements in the set are a subset of another sequence, e.g. a view of the dictionary's keys.

The dict.keys method returns a new view of the dictionary's keys.

main.py
my_dict = { 'name': 'Bobby hadz', 'country': 'Austria', 'age': 30 } # ๐Ÿ‘‡๏ธ dict_keys(['name', 'country', 'age']) print(my_dict.keys())

The less than or equals to sign <= checks if the set object is a subset of the view of the dictionary's keys.

main.py
my_dict = { 'name': 'Bobby Hadz', 'country': 'Austria', 'age': 30 } if {'name', 'country'} <= my_dict.keys(): # ๐Ÿ‘‡๏ธ this runs print('multiple keys are in the dictionary')

# Check if multiple Keys are in a Dictionary using a set.issubset() object

An alternative to using <= is to use the set.issubset() method.

main.py
my_dict = { 'name': 'Bobby Hadz', 'country': 'Austria', 'age': 30 } if {'name', 'country'}.issubset(my_dict.keys()): # ๐Ÿ‘‡๏ธ this runs print('multiple keys are in the dictionary')

check if multiple keys are in dictionary using set issubset

The set.issubset method tests whether every element of the set is in the provided sequence.

# Check if multiple Keys exist in a Dictionary using a for loop

You can also use a for loop to check if multiple keys exist in a dictionary.

main.py
my_dict = { 'name': 'Bobby Hadz', 'country': 'Austria', 'age': 30 } keys = ['name', 'country'] multiple_keys_exist = True for key in keys: if key not in my_dict: multiple_keys_exist = False break if multiple_keys_exist: # ๐Ÿ‘‡๏ธ this runs print('multiple keys are in the dictionary') else: print('At least one key is not in the dictionary')

We initialized the multiple_keys_exist variable to True and used a for loop to iterate over the dictionary.

On each iteration, we check if the current key doesn't exist in the dictionary.

If the condition is met, we set the multiple_keys_exist variable to False and break out of the loop.

The break statement breaks out of the innermost enclosing for or while loop.

# Check if multiple Keys exist in a Dictionary using and operator

If you only need to check for 2 or 3 keys, you can also use the boolean and operator.

main.py
my_dict = { 'name': 'Bobby Hadz', 'country': 'Austria', 'age': 30 } if 'name' in my_dict and 'country' in my_dict: # ๐Ÿ‘‡๏ธ this runs print('multiple keys are in the dictionary') else: print('At least one key is not in the dictionary')

We used the boolean and operator to check for multiple conditions.

The if block is only run if all of the specified keys exist in the dictionary.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev