jeudi 24 novembre 2016

Python if/else multiple conditions in one line [duplicate]

dict = {'shelf':1, 'slot':2}
if 'shelf' and 'slot' in dict.keys():
    ....
else:
    ....

or

if 'shelf' in dict.keys() and 'slot' in dict.keys():
    ....
else:
    ....

I know 2nd code checks both keys are there in dict. 1st code checks condition for 'slot' only.

But can I make first code work with the help of some brackets or replacing and? Its a lot helpful when I have a huge dict with lot of keys.

Aucun commentaire:

Enregistrer un commentaire