Python check if list of keys exist in dictionary

前端 未结 4 1873
失恋的感觉
失恋的感觉 2020-12-23 15:48

I have a dictionary that looks like that:

grades = {
        \'alex\' : 11,
        \'bob\'  : 10,
        \'john\' : 14,
        \'peter\': 7
       }
         


        
4条回答
  •  死守一世寂寞
    2020-12-23 16:32

    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")
    

提交回复
热议问题