lundi 18 janvier 2021

Convert to set Comprehension

I new to Python and im trying to convert this loop with an if else statement in it to a comprehension but just can't seem to crack it...

Hope you guys can help me?

Dict = {'Tim': 18,'Charlie':12,'Tiffany':22,'Robert':25}
Boys = {'Tim': 18,'Charlie':12,'Robert':25}
Girls = {'Tiffany':22}
for key in list(Dict.keys()):
   if key in list(Boys.keys()):
       print(True)
   else:
       print(False)

I've tried the following...

a = {key for key in list(Dict.keys()) if key in list(Boys.keys())}
b = {key if key in list(Boys.keys()) else False for key in list(Dict.keys())}
c = {key in list(Boys.keys()) if key else False for key in list(Dict.keys())}
print(c)

Aucun commentaire:

Enregistrer un commentaire