Last updated: Apr 9, 2024
Reading timeยท4 min
and
operatorTo check if multiple keys are in a dictionary:
in
operator to check if each key is in the dictionary.all()
function.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')
We wrapped the multiple keys we wanted to test for in a tuple
and used a
generator expression
to iterate over the tuple
.
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
.
dict
object.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.
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.
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.
set
objectThis is a three-step process:
set
object.dict.keys()
method to get a view of the dictionary's keys.my_dict = { 'name': 'Bobby Hadz', '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.
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.
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.
my_dict = { 'name': 'Bobby Hadz', 'country': 'Austria', 'age': 30 } if {'name', 'country'} <= my_dict.keys(): # ๐๏ธ this runs print('multiple keys are in the dictionary')
set.issubset()
objectAn alternative to using <=
is to use the set.issubset()
method.
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')
The
set.issubset()
method tests whether every element of the set
is in the provided sequence.
for
loopYou can also use a for
loop to check if multiple keys exist in a dictionary.
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.
and
operatorIf you only need to check for 2 or 3 keys, you can also use the boolean and
operator.
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.
You can learn more about the related topics by checking out the following tutorials: