All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Filter A List In Python

Last Updated : Aug 23, 2023

How To Filter A List 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)
  1. In the beginning, the is_even(num) function is defined.
  2. To determine whether an inputted number num is even, use this function.
  3. It accomplishes this by determining whether num is divisible by 2 using the modulus operator%.
  4. The number is even and the method returns True if the condition num% 2 == 0 evaluates to True. Otherwise, False is returned.
  5. The filter_list(lst) function is then defined. A list named lst is the input for this function.
  6. The elements that satisfy our filtering requirement will be stored in the filtered_list, an empty list that we initialize inside the method.
  7. Using a for loop, we traverse through each item in the input list lst.
  8. We use the is_even() characteristic to determine whether or not every object is even.
  9. 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.
  10. We have a list filtered_list with the filtered elements when the loop is finished. This list is what the filter_list() method returns.
  11. A list called original_list is created in the main section of the code and contains a series of numbers.
  12. 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.
  13. 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.
  14. The original list & the filtered list are then shown using the print() function.
  15. 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.

Conclusion :-

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.