How to check if a key already exists in a Dictionary in Python

The pythonic way to check if a key already exists in a Dictionary in Python is to use the membership operator in.

people = {'Bob':30, 'Mary':25}

if 'Bob' in people:
  print('Bob exists!')
else:
  print('There is no Bob!')
Bob exists!

To learn more about membership operators, checkout this post Membership Operators in Python.