Check if multiple Keys are in a Dictionary in Python

avatar

Borislav Hadzhiev

Last updated: Jul 15, 2022

banner

Photo from Unsplash

Check if multiple Keys are 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': 'Alice', 'country': 'Austria', 'age': 30 } # ✅ check if multiple keys in dict using all() if all(key in my_dict for key in ("name", "country")): # 👇️ this runs print('multiple keys are in the dictionary') # ---------------------------------------------- # ✅ check if multiple keys in dict using set object if {'name', 'country'} <= my_dict.keys(): # 👇️ this runs print('multiple keys are in the dictionary')

We wrapped the multiple keys we want 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': 'Alice', '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': 'Alice', '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 it will return False.

main.py
my_dict = { 'name': 'Alice', '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 set #

To check if multiple keys are in a dictionary:

  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': 'Alice', 'country': 'Austria', 'age': 30 } if {'name', 'country'} <= my_dict.keys(): # 👇️ this runs print('multiple keys are in the dictionary')

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': 'Alice', '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': 'Alice', 'country': 'Austria', 'age': 30 } if {'name', 'country'} <= my_dict.keys(): # 👇️ this runs print('multiple keys are in the dictionary')

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

main.py
my_dict = { 'name': 'Alice', 'country': 'Austria', 'age': 30 } if {'name', 'country'}.issubset(my_dict.keys()): # 👇️ this runs print('multiple keys are in the dictionary')

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

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.