Find object in a List of objects in Python

avatar

Borislav Hadzhiev

Last updated: Sep 15, 2022

banner

Photo from Unsplash

Table of Contents #

  1. Find an object in a List of objects in Python
  2. Find all objects in a list that meet a condition

Find an object in a List of objects in Python #

To find an object in a list of objects:

  1. Use a generator expression to iterate over the list.
  2. Check if each object has an attribute that meets a condition.
  3. Use the next() function to return the first matching object.
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 a 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
The code sample finds the first object in the list that meets a condition. If you need to find all objects that meet a certain condition in a list, scroll down to the next subheading.

We used a generator expression to iterate over the list of objects.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

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 simple for loop.

To find an object in a list of objects:

  1. Use a for loop to iterate over the list.
  2. Check if each object has an attribute that meets a condition.
  3. Use the break statement to break out of the loop once you find a matching object.
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] match = None for obj in list_of_objects: if obj.name == 'Bobbyhadz': match = obj break print(match) # 👉️ Bobbyhadz if match is not None: print(match.name) # 👉️ Bobbyhadz print(match.salary) # 👉️ 75

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

Once we find a matching object, we reassign the match variable and break out of the loop.

The break statement breaks out of the innermost enclosing for or while loop.

Find all objects in a list that meet a condition #

To find all objects in a list that meet a condition:

  1. Use a list comprehension to iterate over the list.
  2. Check if each object has an attribute that meets a condition.
  3. The new list will only contain the matching objects.
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] result = [obj for obj in list_of_objects if obj.salary == 75] print(result) # 👉️ [Bobbyhadz, Carl]

We used a list comprehension to iterate over the list of objects.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the current object has a salary attribute equal to 75 and return the result.

Alternatively, you can use a for loop.

To find all objects in a list that meet a condition:

  1. Use a for loop to iterate over the list.
  2. Check if each object has an attribute that meets a condition.
  3. Append the matching objects to a new list.
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] matches = [] for obj in list_of_objects: if obj.salary == 75: matches.append(obj) print(matches) # 👉️ [Bobbyhadz, Carl]

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

On each iteration, we check if the current object has a salary attribute equal to 75 and if the condition is met, we append the object to the matches list.

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.