Python filter() built-in function

From the Python 3 documentation

Construct an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.

Introduction

The filter method in Python is a built-in function that allows you to filter a sequence (e.g., a list, a tuple, etc.) by applying a certain condition to each element in the sequence. The filter method returns a new sequence containing only the elements that meet the specified condition.

Here is a simple example of using the filter method to filter a list of numbers and keep only the even numbers:

Example

>>> def is_even(num):
...     return num % 2 == 0
...
>>> numbers = [1, 2, 3, 4, 5, 6]
>>> even_numbers = list(filter(is_even, numbers))
>>> print(even_numbers)
>>> [2, 4, 6]

In this example, the is_even function is defined to determine whether a number is even or not. The filter method takes two arguments: the first argument is the function to apply to each element of the list, and the second argument is the list to be filtered. The filter method returns an iterable, which is then converted to a list and stored in the even_numbers variable. The final output is the list of even numbers from the original list.

Subscribe to pythoncheatsheet.org

Join 10.900+ Python developers in a two times a month and bullshit free publication , full of interesting, relevant links.