Filter [extract / remove] items of a list with filter[] in Python
In Python, you can use filter[] to filter [extract / remove] items of a list, tuple, or other iterable that satisfy the conditions.
- Built-in Functions - filter[] Python 3.9.7 documentation
This article describes the following contents.
- Basic usage of filter[]
- filter[] returns an iterator in Python3
- Convert to a list
- Extract and remove elements according to conditions
- Apply lambda expressions [lambda] and functions defined with def
- Apply multiple conditions
- Specify None as the first argument
- Extract False elements: itertools.filterfalse[]
- Use list comprehensions and generator expressions instead
Note that filter[] can be substituted by list comprehensions or generator expressions, as described later, and in many cases it is preferable to use them.
- List comprehensions in Python
For more information on extracting elements of a list using list comprehensions, see the following article.
- Extract, replace, convert elements of a list in Python
Basic usage of filter[]
The first argument of filter[] is a callable object such as a function to be applied, and the second argument is an iterable object such as a list. Apply the function to the iterable elements and extract the ones whose result is determined to be True.
filter[] returns an iterator in Python3
For example, use the lambda expression that returns True if the value is an even number [= the remainder of the value divided by 2 is 0].
- Lambda expressions in Python
In Python 3, filter[] returns an object of type filter which is an iterator, and print[] does not output items.
The value of the iterator can be retrieved with a for statement.
- for loop in Python [with range, enumerate, zip, etc.]
Note that filter[] in Python 2 returns a list, so be careful when running Python 2 code in Python 3.
- 2. Built-in Functions - filter[] Python 2.7.18 documentation
Convert to a list
If you want to convert the result of filter[] to a list, use list[].
Extract and remove elements according to conditions
filter[] extracts elements whose result is determined to be True by applying the function of the first argument. If you want to remove an element, specify a function whose result is the opposite.
For example, "remove even elements" is equivalent to "extract odd elements".
As in the example above, you can replace the comparison operators with ones that produce opposite results [== and ! =, > and 0, l]]] # [2] print[list[filter[lambda x: x % 2 == 0 or x > 0, l]]] # [-2, 0, 1, 2]
Specify None as the first argument
If None is specified as the first argument of filter[], elements which are determined to be True [= truthy elements] are extracted.
Not only True and False, but also numbers, lists, and strings are determined.
For example, in the case of numbers, 0 is regarded as False and the others are regarded as True, in the case of lists and strings, if it is empty, it is regarded as False and the others are regarded as True.
See the following article for details.
- Convert bool [True, False] and other types to each other in Python
Therefore, if you specify None as the first argument of filter[], 0 and empty list / string is removed.
Extract False elements: itertools.filterfalse[]
Contrary to filter[], a function itertools.filterfalse[] is also provided to keep elements that are False.
- itertools.filterfalse[] Functions creating iterators for efficient looping Python 3.9.7 documentation
Usage is the same as filter[]. You need to import itertools.
As mentioned above, the same result can be obtained with filter[] by changing the function of the first argument, but in some cases, itertools.filterfalse[] can be used to write code whose intention is clearer than using filter[] and not.
If the first argument of itertools.filterfalse[] is set to None, elements which are determined to be False [= falsy elements] are extracted.
Use list comprehensions and generator expressions instead
The equivalent of filter[] can also be achieved with list comprehensions and generator expressions.
Note that filter[function, iterable] is equivalent to the generator expression [item for item in iterable if function[item]] if function is not None and [item for item in iterable if item] if function is None. Built-in Functions - filter[] Python 3.9.7 documentation
If you want to get a list like list[filter[]], use the list comprehensions, and if you want to get an iterator like filter[], use a generator expression.
- List comprehensions in Python
As shown in the following Stack Overflow question, in most cases, using list comprehensions and generator expressions is preferable to filter[] because the code is more concise and clear.
- python - List comprehension vs. lambda + filter - Stack Overflow
Note that, since processing speed may vary due to various factors, if speed is important to you, it is recommended to measure it under conditions as close to your assumptions as possible.
- Measure execution time with timeit in Python