All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Filter List Of Objects

Last Updated : Mar 11, 2024

Python Filter List Of Objects

In this article we will show you the solution of python filter list of objects, you can choose to extract elements from a list of objects in Python by filtering the collection according to predefined constraints or parameters.

For this, the filter() function is often used. It requires arguments: an iterable (like a list) containing the items to be filtered and a function that specifies the filtering condition.

When a detail satisfies the specified criterion, the filtering characteristic ought to go back True, and whilst it does now not, it should go back False.

When this characteristic & the iterable are passed out to filter (), simplest the items that meet the criterion are returned inside the iterator.

Python's filtering of lists of objects offers a potent method for removing particular elements depending on specific criteria, making it a flexible tool for data manipulation & analysis. Now move to the concept of python filter list of objects.

Step By Step Guide On Python Filter List Of Objects :-

class Employee:
    def __init__(self, name, age, designation):
        self.name = name
        self.age = age
        self.designation = designation
# Creating a list of employee objects
employees = [
    Employee("John", 25, "Manager"),
    Employee("Alice", 28, "Employee"),
    Employee("Bob", 32, "Manager"),
    Employee("Eve", 30, "Employee"),
]
# Filtering employees who are managers
filtered_employees = list(filter(lambda emp: emp.designation == "Manager", employees))
# Printing the filtered employees
for emp in filtered_employees:
    print(f"Name: {emp.name}, Age: {emp.age}, Designation: {emp.designation}")
  1. With characteristics like name, age, and designation, the Employee class in this code represents an employee object.
  2. For each employee, an instance of this class is created.
  3. We first create a list called employees using the Employee class.
  4. We initialize many Employee objects with various names, ages, & designations inside the list.
  5. The list of employees will then be filtered based on a specific set of criteria.
  6. In this instance, we only want to keep the employees with the title "Manager" in the system.
  7. We make use of the filter() function to accomplish this.
  8. An iterable and a filtering function are the two arguments that the filter() function accepts.
  9. As the filtering function in our code, we offer a lambda function.
  10. The emp parameter, which represents a specific employee object from the employees list, is provided to this lambda function.
  11. It determines whether the employee's designation attribute is set to "Manager".
  12. The lambda function returns True in this case, indicating that the employee should be included to the filtered list.
  13. Otherwise, False is returned.
  14. The filter() function receives the lambda function & the employees list as parameters.
  15. Each employee object in the list is subject to the lambda function using the filter() method, which then produces an iterator that only contains the objects for whom the lambda function returned True.
  16. We wrap the filter() function in the list() function to turn this iterator into a list, and then we give the result to the filtered_employees variable.
  17. Finally, a for loop is used to traverse through the list of filtered employees.
  18. We use dot notation to access the name, age, and designation attributes of each employee object in each iteration and an f-string to print them out.

Conclusion :-

As a result, we were able to understand the idea of a Python filter list of objects.

We additionally discovered that the usage of Python's filter() feature to filter a list of objects offers us a flexible and powerful way to extract specific factors primarily based on our very own criteria.

A filtering function can be created after which implemented to an iterable, together with a listing of items, to create a brand new listing that simplest consists of the items that in match the situation.

I hope this article on python filter list of objects helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪