montegu
montegu

Reputation: 1

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

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

Answers (1)

CodeKorn
CodeKorn

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

Related Questions