Python filter() Function

The filter() is a built-in function in Python that is used to extract the elements from iterable objects like lists, sets, tuples e.t.c. This function takes two arguments; function argument that tests each element in the sequence to be true or not and the iterable argument where the function is applied to each element from the iterable.

Related: Different ways to Filter Elements from List

Here, I will cover the basics of the filter() function and use it with the lambda function, custom function e.t.c

1. Python filter() Syntax

Following is the syntax of the filter() function.


# Syntax of filter()
filter(function, iterable)

1.1 Parameter

  • function – A function is a callable object (i.e. a function, method, or lambda expression) that will be applied to each item of the iterable (e.g. list, sets, tuples, etc.).
  • iterable – It is iterable which is the collection of items on which the function will be applied.

1.2 Return

It returns an iterator after filtering the elements.

2. Python filter() Function with Lambda Expression

The filter() is a built-in function in Python that is used to filter the elements from the iterable object like listset, tuple e.t.c. This function will directly filter the elements in an iterable by taking the condition as a function. We can specify the conditions using lambda expressions/custom functions etc.

The below example uses the lambda function with the filter() function. And use the list() function to convert the result of the filter() to the list.


# Consider the list of integers
scores=[100,200,300,400,500]
print("Actual List: ",scores)

# Using filter() with lambda expression
print("Greater than 100: ",list(filter(lambda i: i > 100, scores)))
print("Less than 100: ",list(filter(lambda i: i < 100, scores)))

Yields below output.

python filter function

Here, the first example returns the integers greater than 100 and the second example returns the integers less than 100. since there are no integers less than 100, an empty list is returned.

3. Using Custom Function with filter()

We can also use the filter() by calling the function for each element of the iterable object. Here, I used the same example above but rewrite it using the custom function and calling it from the filter().


# Consider the list of integers
scores=[100,200,300,400,500]
print("Actual List: ",scores)

# Function that return the elements that are greater than 100.
def filter1(i):
    return i > 100

# Function that return the elements that are less than 100.
def filter2(i):
    return i < 100

# Using filter() with user defined function
print("Greater than 100: ",list(filter(filter1, scores)))
print("Less than 100: ",list(filter(filter2, scores)))

# Output:
# Actual List:  [100, 200, 300, 400, 500]
# Greater than 100:  [200, 300, 400, 500]
# Less than 100:  []

Here, the first function name is filter1, which will return the integers greater than 100. There are 4 integers and the second function name is filter2, which will return the integers less than 100. There is no integer less than 100. So empty list is returned.

4. Using None for Function

You can also use the None as a value to the function argument in filter(). When None is used, it returns all values except False/None/0 values from the iterable. All these values are considered False in Python.

In the below example, I have used the tuple as an iterator to the filter() function.


# Create tuple
values=(1, False, True, None, 'Str', 0, -1)
print("Actual List: ",values)

# Using None with filter()
print("Result: ",list(filter(None, values)))

# Output:
# Actual List:  (1, False, True, None, 'Str', 0, -1)
# Result:  [1, True, 'Str', -1]

Conclusion

In this article, you have learned the syntax and usage of the filter() function in Python. This function is used to filter the elements from the iterable by evaluating the function to true. For function, you can either use the lambda expression or call the custom function. The function is called for each element of the iterable.

For more examples of functions refer to Python Built-in Functions.

References

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply