Check if list of keys exist in dictionary [duplicate]
Solution 1:
Use all()
:
if all(name in grades for name in students):
# whatever
Solution 2:
>>> 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
.
Solution 3:
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")