How To Filter A List In Python
Last Updated : Aug 23, 2023
IN - Python
In this article we will show you the solution of how to filter a list in python, filtering a list in Python gives you the potential to handiest extract additives that in shape certain necessities.
A condition is implemented to every object in the list as a part of the manner, and a fresh new list is then created that simplest includes the objects that meet the condition.
This operation is made less difficult with the aid of the built-in filter() characteristic that Python offers.
You can use a lambda function or a regular function to define a condition to filter a list.
For the elements you want to include in the filtered list, the condition should return True.
Then, you supply the filter() method with the original list and this condition. Now move to the concept of how to filter a list in python.
Step By Step Guide On How To Filter A List In Python :-
def is_even(num): return num % 2 == 0 def filter_list(lst): filtered_list =  for item in lst: if is_even(item): filtered_list.append(item) return filtered_list original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] filtered_list = filter_list(original_list) print("Original List:", original_list) print("Filtered List (Even Numbers):", filtered_list)
- In the beginning, the is_even(num) function is defined.
- To determine whether an inputted number num is even, use this function.
- It accomplishes this by determining whether num is divisible by 2 using the modulus operator%.
- The number is even and the method returns True if the condition num% 2 == 0 evaluates to True. Otherwise, False is returned.
- The filter_list(lst) function is then defined. A list named lst is the input for this function.
- The elements that satisfy our filtering requirement will be stored in the filtered_list, an empty list that we initialize inside the method.
- Using a for loop, we traverse through each item in the input list lst.
- We use the is_even() characteristic to determine whether or not every object is even.
- The item is delivered to the filtered_list using the append() approach if the is_even() feature returns True, indicating that the object is even.
- We have a list filtered_list with the filtered elements when the loop is finished. This list is what the filter_list() method returns.
- A list called original_list is created in the main section of the code and contains a series of numbers.
- We use the original_list as an argument when we execute the filter_list() method, and we save the filtered list that results in the variable filtered_list.
- By doing this, the filtering procedure is carried out, and a new list that only includes the even numbers from the initial list is produced.
- The original list & the filtered list are then shown using the print() function.
- We can see both the original list of numbers as well as the filtered list, which only includes the even numbers from the original list, by printing these lists.
As a result, we have successfully acquired the knowledge necessary to filter a list in Python.
We also discovered that using Python's filtering capabilities, we can extract particular components depending on our own criteria.
By creating a condition using a lambda function or a regular function using the built-in filter() function, we may accomplish this.
The procedure entails going through the initial list repeatedly, applying the condition to every member, and making a new list of the components that meet the condition.
I hope this article on how to filter a list in python helps you and the steps and method mentioned above are easy to follow and implement.