Name

filter

Synopsis

filter(func,seq)

Constructs a list from those elements of seq for which func is true. func can be any callable object that accepts a single argument or None. seq must be a sequence, iterator, or other iterable object. When func is a callable object, filter calls func on each item of seq and returns the list of items for which func’s result is true, like this:

[item for item in seq if func(item)]

When seq is a string or tuple, filter’s result is also a string or tuple, rather than a list. When func is None, filter tests for true items like this:

[item for item in seq if item]

Get Python in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.