Check if object exists in a List of objects in Python

avatar

Borislav Hadzhiev

Last updated: Sep 15, 2022

banner

Photo from Unsplash

Check if object exists in a List of objects in Python #

Use the any() function to check if an object exists in a list of objects. The any() function will return True if the object exists in the list, otherwise False is returned.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def __repr__(self): return self.name alice = Employee('Alice', 100) bob = Employee('Bobbyhadz', 75) carl = Employee('Carl', 75) list_of_objects = [alice, bob, carl] if any(obj.name == 'Bobbyhadz' for obj in list_of_objects): # 👇️ this runs print('Object exists in list') else: print('Object does NOT exist in list')

We used the any() function to check if a list of objects contains a specific object.

The any function takes an iterable as an argument and returns True if any element in the iterable is truthy.

main.py
my_list = ['alice', 'bobbyhadz', None, 'carl'] # 👇️ True print(any(item is None for item in my_list))
If the iterable is empty or none of the elements in the iterable are truthy, the any function returns False.

We passed a generator expression to the any() function.

main.py
list_of_objects = [alice, bob, carl] if any(obj.name == 'Bobbyhadz' for obj in list_of_objects): # 👇️ this runs print('Object exists in list') else: print('Object does NOT exist in list')
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

We check if each object has a name attribute equal to a specific value and return the result.

If the condition is met, the any() function returns True and the if block runs.

If the condition is never met, the object is not contained in the list and the else block runs.

If you need to get the matching object, use the next() function.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def __repr__(self): return self.name alice = Employee('Alice', 100) bob = Employee('Bobbyhadz', 75) carl = Employee('Carl', 75) list_of_objects = [alice, bob, carl] # ✅ Get first object in list that meets the condition result = next( (obj for obj in list_of_objects if obj.name == 'Bobbyhadz'), None ) print(result) # 👉️ Bobbyhadz print(result.name) # 👉️ Bobbyhadz print(result.salary) # 👉️ 75

On each iteration, we check if the current object has a name attribute with a specific value and return the result.

When we pass an iterator to the next() function, the next item in the stream is returned.

We also specified a None default value, which is returned if none of the objects in the list meet the condition.

Alternatively, you can use a for loop to iterate over the list of objects and check if each object meets the specific condition.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def __repr__(self): return self.name alice = Employee('Alice', 100) bob = Employee('Bobbyhadz', 75) carl = Employee('Carl', 75) list_of_objects = [alice, bob, carl] obj_is_in_list = False for obj in list_of_objects: if obj.name == 'Bobbyhadz': obj_is_in_list = True print(obj_is_in_list) # 👉️ True if obj_is_in_list: print('Object exists in list') else: print('Object does NOT exist in list')

We used a for loop to iterate over the list of objects.

On each iteration, we check if the current object has a name attribute equal to a specific string.

If the condition is met, the object is contained in the list and we reassign the obj_is_in_list variable to True.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.