how to check membership of keys and values a dictionaries in python with user defined function

51 Views Asked by At

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

1

There are 1 best solutions below

0
CodeKorn On

Here's a one liner:

def key_in_dict(d, key):
    return 'Exists!' if key in d else "Doesn't exist!"