Javatpoint Logo
Javatpoint Logo

Python filter() Function

Python's built-in filter() function is a powerful too; for performing data filtering procedure on sequences like lists, tuples, and strings. The filter() function is utilized to apply a function to each element of an iterable (like a list or tuple) and return another iterable containing just the elements for which the function brings True back. Along these lines, filter() permits us to filter out elements from a grouping based on some condition. The first argument can be None if the function is not available and returns only elements that are True.

Syntax:

Parameters

function: It is a function. If set to None returns only elements that are True.

Iterable: Any iterable sequence like list, tuple, and string.

Both the parameters are required.

Return

It returns the same as returned by the function.

Let's see some examples of filter() function to understand it's functionality.

Python filter() Function Example 1

This simple example returns values higher than 5 using filter function. See the below example.

Code

Output:

[6]

The code defines a function called filterdata that takes a single argument x. In this function, we check assuming x is greater than 5, and provided that this is true, we print x. Then, we call the filter() function, passing in filterdata as the main argument and a tuple (1, 2, 6) as the subsequent argument. The filter() function applies the filterdata() function to every element of the tuple and returns an iterator that contains just the elements that fulfill the condition x > 5. At last, we convert the iterator into a list utilizing the list() function and print the outcome.

The following are a couple of additional examples of how to utilize filter() method in Python:

Example 2:

Filter out empty strings from a list:

Code

Output:

[ 'hello', 'world' ]

In this example, the lambda function checks in the event that each string isn't empty by testing assuming it is equivalent to the empty string (''). The filter() function returns another iterable containing just the non-empty strings.

Example 3:

Filter out negative numbers from a list:

Code

Output:

[1, 3, 5]

In this example, the lambda function checks assuming each number is non-negative by testing in the event that it is more prominent than or equivalent to zero (0). The filter() function returns another iterable containing just the non-negative numbers.

Example 4:

Filter out duplicates from a list:

Code

Output:

[ 1, 4, 5 ]

In this example, the lambda function checks in the event that each number shows up just a single time in the numbers list by counting how often it shows up (numbers.count(x)) and testing in the event that the count is equivalent to 1. The filter() function returns another iterable containing just the unique numbers.

Note: It's significant that in Python 3, the filter() function returns an iterable, and that implies that you really want to change it over completely to a list (as displayed in the examples above) to involve it as a list. Nonetheless, in Python 2, the filter() function returns a list of course.

Conclusion:

Finally, the filter() function is a powerful tool for data manipulation in Python. By filtering an iterable based on a predefined condition, you can rapidly and effectively separate the data that you really want. You can likewise utilize filter() in blend with other Python functions to perform more perplexing data manipulations.


Next TopicPython Functions





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA