Returns a sequence from those elements of iterable for which function returns True.
filter (function, iterable)
- Required. A function. If set to None returns only elements that are True.
- Required. Any iterable sequence.
same as returned by function
Note that filter(function, iterable) is equivalent to [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.
See itertools.ifilter() and itertools.ifilterfalse() for iterator versions of this function, including a variation that filters for elements where the function returns False.
>>> def f(x): ... if x > 0: ... return x >>> filter(f, [-1, 0, 1]) 
>>> filter(None, (0, 1, True)) (1, True)