How to check if multiple keys exists in a dictionary in Python

2 Answers

0 votes
dict = { 'python': 5, 
         'php': 3, 
         'java': 9, 
         'c++': 1 }
       
if all(key in dict for key in ("python", "java")):
    print('yes')
else:
    print('no')




'''
run:

yes

'''

 



answered Jul 28, 2022 by avibootz
0 votes
dict = { 'python': 5, 
         'c': 3, 
         'java': 9, 
         'c++': 1 }
       
if {'python', 'c++', 'c'} <= dict.keys():
    print('yes')
else:
    print('no')




'''
run:

yes

'''

 



answered Jul 28, 2022 by avibootz
...