Python filter Function with Examples

The built-in filter function in Python serves as an effective method for executing data filtering operations on various sequences, such as lists, tuples, and strings. This function is designed to apply a specified function to each item within an iterable (for instance, a list or tuple) and yields a new iterable that consists solely of the elements for which the provided function evaluates to True. Consequently, filter enables users to eliminate elements from a collection based on a specific criterion. If there is no available function, the initial argument can be set to None, resulting in the return of only those elements that are deemed True.

Python filter Function Syntax

It has the following syntax:

Example

filter (function, iterable)

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.

Different Examples for Python filter Function

Let us explore a few examples of the filter function to gain a clearer understanding of its capabilities.

Python filter Function Example 1

The following straightforward example demonstrates how to retrieve values that exceed 5 by utilizing the filter function. Refer to the example provided below.

Example

# Python filter() function example

def filterdata(x):

    if x>5:

        return x

# Calling function

result = filter(filterdata,(1,2,6))

# Displaying result

print(list(result))

Output:

Explanation:

The provided code snippet defines a function named filterdata that accepts one parameter, x. Within this function, we evaluate whether x exceeds 5; if this condition holds true, we proceed to print the value of x. Subsequently, we invoke the filter function, supplying filterdata as the primary argument alongside a tuple (1, 2, 6) as the second argument. The filter function executes the filterdata function on each element of the tuple, producing an iterator that retains only those elements satisfying the condition x > 5. Finally, we transform the iterator into a list by employing the list function and display the result.

Here are a few more instances illustrating the use of the filter function in Python:

Python filter Function Example 2:

In this section, we will demonstrate how to eliminate empty strings from a list in Python.

Example

# Python filter() function example

strings = [ '', 'hello', '', 'world', '' ]

# Using filter() to remove empty strings from the list

result = list(filter(lambda x: x != '', strings))

# Printing the result

print(result)

Output:

Output

[ 'hello', 'world' ]

Explanation:

In this instance, the lambda function evaluates whether each string is not empty by comparing it to the empty string (''). The filter function produces an additional iterable that includes only the strings that are non-empty.

Python filter Function Example 3:

Consider an example that illustrates the process of removing negative numbers from a collection of values.

Example

# Python filter() function example

numbers = [ 1, -2, 3, -4, 5, -6 ]

# Using filter() to remove negative numbers from the list

result = list(filter(lambda x: x >= 0, numbers))

# Printing the result

print(result)

Output:

Output

[1, 3, 5]

Explanation:

In this illustration, the lambda function evaluates the premise that every number is non-negative by verifying if it is greater than or equal to zero (0). The filter function yields a new iterable that comprises solely the non-negative numbers.

Python filter Function Example 4:

Let us consider an example to eliminate duplicate entries from a list.

Example

# Python filter() function example

numbers = [ 1, 2, 3, 2, 4, 3, 5 ]

# Using filter() to remove duplicate numbers from the list

result = list(filter(lambda x: numbers.count(x) == 1, numbers))

# Printing the result

print(result)

Output:

Output

[ 1, 4, 5 ]

Explanation:

In this illustration, the lambda function verifies whether each number appears only once within the numbers list by evaluating the frequency of its occurrence (numbers.count(x)) and checking if this count is equal to 1. The filter function produces a new iterable that includes solely the distinct 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

Ultimately, the filter function serves as a robust instrument for data manipulation within Python. By applying a filter to an iterable according to a specified condition, you can efficiently and swiftly isolate the data that you truly need. Additionally, you can combine filter with other Python functions to execute more intricate data manipulations.

Input Required

This code uses input(). Please provide values below: