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}")
- With characteristics like name, age, and designation, the Employee class in this code represents an employee object.
- For each employee, an instance of this class is created.
- We first create a list called employees using the Employee class.
- We initialize many Employee objects with various names, ages, & designations inside the list.
- The list of employees will then be filtered based on a specific set of criteria.
- In this instance, we only want to keep the employees with the title "Manager" in the system.
- We make use of the filter() function to accomplish this.
- An iterable and a filtering function are the two arguments that the filter() function accepts.
- As the filtering function in our code, we offer a lambda function.
- The emp parameter, which represents a specific employee object from the employees list, is provided to this lambda function.
- It determines whether the employee's designation attribute is set to "Manager".
- The lambda function returns True in this case, indicating that the employee should be included to the filtered list.
- Otherwise, False is returned.
- The filter() function receives the lambda function & the employees list as parameters.
- 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.
- 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.
- Finally, a for loop is used to traverse through the list of filtered employees.
- 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.