Check if list of keys exist in dictionary
Question:
I have a dictionary that looks like that:
grades = {
'alex' : 11,
'bob' : 10,
'john' : 14,
'peter': 7
}
and a list of names students = ('alex', 'john')
I need to check that all the names in students
exist as keys in grades
dict.
grades
can have more names, but all the names in students
should be in grades
There must be a straightforward way to do it, but i’m still new to python and can’t figure it out. tried if students in grades
, didn’t work.
In the actual cases, the lists will be much bigger.
Answers:
Use all()
:
if all(name in grades for name in students):
# whatever
>>> grades = {
'alex' : 11,
'bob' : 10,
'john' : 14,
'peter': 7
}
>>> names = ('alex', 'john')
>>> set(names).issubset(grades)
True
>>> names = ('ben', 'tom')
>>> set(names).issubset(grades)
False
Calling it class
is invalid so I changed it to names
.
Assuming students as set
if not (students - grades.keys()):
print("All keys exist")
If not convert it to set
if not (set(students) - grades.keys()):
print("All keys exist")
You can test if a number of keys are in a dict by taking advantage that <dict>.keys()
returns a set
.
This logic in code…
if 'foo' in d and 'bar' in d and 'baz' in d:
do_something()
can be represented more briefly as:
if {'foo', 'bar', 'baz'} <= d.keys():
do_something()
The <=
operator for sets tests for whether the set on the left is a subset of the set on the right. Another way of writing this would be <set>.issubset(other)
.
There are other interesting operations supported by sets: https://docs.python.org/3.8/library/stdtypes.html#set
Using this trick can condense a lot of places in code that check for several keys as shown in the first example above.
Whole lists of keys could also be checked for using <=
:
if set(students) <= grades.keys():
print("All studends listed have grades in your class.")
# or using unpacking - which is actually faster than using set()
if {*students} <= grades.keys():
...
Or if students
is also a dict:
if students.keys() <= grades.keys():
...
I have a dictionary that looks like that:
grades = {
'alex' : 11,
'bob' : 10,
'john' : 14,
'peter': 7
}
and a list of names students = ('alex', 'john')
I need to check that all the names in students
exist as keys in grades
dict.
grades
can have more names, but all the names in students
should be in grades
There must be a straightforward way to do it, but i’m still new to python and can’t figure it out. tried if students in grades
, didn’t work.
In the actual cases, the lists will be much bigger.
Use all()
:
if all(name in grades for name in students):
# whatever
>>> grades = {
'alex' : 11,
'bob' : 10,
'john' : 14,
'peter': 7
}
>>> names = ('alex', 'john')
>>> set(names).issubset(grades)
True
>>> names = ('ben', 'tom')
>>> set(names).issubset(grades)
False
Calling it class
is invalid so I changed it to names
.
Assuming students as set
if not (students - grades.keys()):
print("All keys exist")
If not convert it to set
if not (set(students) - grades.keys()):
print("All keys exist")
You can test if a number of keys are in a dict by taking advantage that <dict>.keys()
returns a set
.
This logic in code…
if 'foo' in d and 'bar' in d and 'baz' in d:
do_something()
can be represented more briefly as:
if {'foo', 'bar', 'baz'} <= d.keys():
do_something()
The <=
operator for sets tests for whether the set on the left is a subset of the set on the right. Another way of writing this would be <set>.issubset(other)
.
There are other interesting operations supported by sets: https://docs.python.org/3.8/library/stdtypes.html#set
Using this trick can condense a lot of places in code that check for several keys as shown in the first example above.
Whole lists of keys could also be checked for using <=
:
if set(students) <= grades.keys():
print("All studends listed have grades in your class.")
# or using unpacking - which is actually faster than using set()
if {*students} <= grades.keys():
...
Or if students
is also a dict:
if students.keys() <= grades.keys():
...