Built-in Function filter
- VC Healy
- May 24, 2020
- 1 min read
filter(function,iterable)
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.
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.
itertools.filterfalse(predicate,iterable
Make an iterator that filters elements from iterable returning only those for which the predicate is False.
If predicate is None, return the items that are false.
Roughly equivalent to:
def filterfalse(predicate, iterable):
# filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8
if predicate is None:
predicate = bool
for x in iterable:
if not predicate(x):
yield x
Comments