filter()

A built-in that keeps the items of an iterable for which a function returns a true value.

In more detail

filter(f, items) returns a lazy iterator of the items where f(item) is true. Passing None instead of a function keeps the items that are true in themselves, dropping 0, empty strings and None. Its results are used up after one pass, like map's.

Example

print(list(filter(None, [0, 1, '', 'a'])))   # [1, 'a']

Related terms

Taught in

References

Certification courses