Python check if list of keys exist in dictionary
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")