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': 'dan@tutorialsbuddy.com'})
customers.append({'firstname': 'Sam', 'email': 'sam@tutorialsbuddy.com'})
customers.append({'firstname': 'Peter', 'email': 'pet@tutorialsbuddy.com'})

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