Check if a Value Already Exists in a List of Dictionary Objects in Python

The following code is an example for checking if a value already exists in a list of dictionary objects in Python:


customers = []
customers.append({'firstname': 'Danny', 'email': '[email protected]'})
customers.append({'firstname': 'Sam', 'email': '[email protected]'})
customers.append({'firstname': 'Peter', 'email': '[email protected]'})

if any(c['email'] == '[email protected]' for c in customers):
    print("Sam exists!")
else:
    print('Sam does not exist!')
Output:
Sam exists!