filter() Function in Python: Be Choosy about Your Elements
Filters are one of the greatest inventions of our century, be it the filters in Snapchat or the filters that are used to cleanse water or the filter that can be deployed in Python to filter out the elements that one is in need of.
Yes, you heard that right. There is a specific function in Python known as 'filter()' function or method that enables you to filter out the elements that you need in a list. In this article, you are going to learn more about it in-depth so without further delay, let's get started.
filter() Function in Python:
Filtering a list based on specific conditions is a common task in Python programming, and the 'filter()' function provides an elegant solution for this purpose. With the 'filter()' function, you can selectively extract elements from an iterable, such as a list, based on a defined filtering condition.
By specifying a filtering function or lambda expression, the filter() function constructs a new iterable containing only the elements that satisfy the condition. This functionality offers a powerful and efficient way to manipulate data and extract relevant information.
Syntax for filter() Function in Python:
The syntax for 'filter()' function in Python is as follows:
>>> filter(function, iterable)
Here's a breakdown of the components:
- 'filter': This is the function that determines the filtering condition for each element in the list.
- 'function': This is the function that you want to apply to each element in the 'iterable'. It can be a built-in function, a user-defined function, or a lambda function.
- 'iterable': This refers to the sequence or iterable objects, such as a list, tuple, or string, that you want to transform. The filter() function will iterate over each element in the 'iterable' and apply the 'function' to it.
The 'filter()' function returns a filter object, which is an iterator that yields the elements from the 'iterable' that satisfy the filtering condition. To access the filtered elements, you can convert the filter object to a list or iterate over it using a loop.
Example Programs using filter() Function in Python:
You have learned about the syntax in the previous section and it is understandable if you feel confused all about it but don't worry, I will explain the learned concept with example programs:
Example 1: Filter out all the positive integers in a list:
>>> def is_positive(number):
>>> return number > 0
>>> numbers = [-2, -1, 0, 1, 2, 3, 4, 5]
>>> positive_numbers = filter(is_positive, numbers)
In this example, the 'is_positive()' function is defined to check if a number is positive by comparing it to zero. The 'numbers' list contains the elements to be filtered. The 'filter()' function is used with 'is_positive' as the function and numbers as the iterable. The 'positive_numbers' variable will hold the filter object that contains the filtered elements.
To access the filtered elements, you can convert the 'positive_numbers' object to a list:
>>> filtered_list = list(positive_numbers)
>>> print(filtered_list)
[1, 2, 3, 4, 5]
In this case, the filter object is converted to a list using the 'list()' function, and the resulting list, 'filtered_list', will contain only the elements from the 'numbers' list that satisfy the 'is_positive()' condition.
Example 2: Filter out all the even numbers in a list:
>>> def is_even(number):
>>> return number % 2 == 0
>>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> even_numbers = list(filter(is_even, numbers))
>>> print(even_numbers)
[2, 4, 6, 8, 10]
In this example, we define the function 'is_even()' that checks whether a number is even by performing the modulo operation '%2' and comparing the result to zero. We have a list of numbers, numbers, and we want to filter out only the even numbers from this list using the 'is_even()' function.
By using the 'filter()' function, we pass the 'is_even' function as the first argument and numbers as the second argument. The 'filter()' function applies the 'is_even()' function to each element in numbers and returns a new iterable containing only the elements for which the function returns 'True'. We convert this iterable to a list using the 'list()' function and store it in 'even_numbers'. Finally, we print the filtered list of even numbers.
Example 3: filter() function using lambda function
The filter() function can also be used with lambda functions, which are anonymous functions that don't require a separate definition. Here's an example using a lambda function to filter out odd numbers from a list:
>>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> odd_numbers = list(filter(lambda x: x % 2 != 0, numbers))
>>> print(odd_numbers)
[1, 3, 5, 7, 9]
In this example, we use a lambda function to check if a number is odd by evaluating the expression 'x%2 != 0'. We pass this lambda function as the first argument to the 'filter()' function, and it filters out only the odd numbers from the 'numbers' list.
Conclusion:
In conclusion, the 'filter()' function in Python serves as a valuable tool for selectively extracting elements from an iterable based on a specific condition. By providing a filtering function or lambda expression, you can easily define the criteria for inclusion or exclusion of elements. Understanding the syntax and usage of the 'filter()' function allows you to streamline your code and make data filtering tasks more manageable and expressive.