Reputation: 1
something like this but in a defined function
# Check if a key exists in a dictionary
info = {'Breakfast': 'Egg', 'time' :'05:30 am'}
if 'lunch' in info.keys():
print('Exists!')
else:
print("Doesn't exist!")
Upvotes: 0
Views: 62
Reputation: 298
Here's a one liner:
def key_in_dict(d, key):
return 'Exists!' if key in d else "Doesn't exist!"
Upvotes: 0