This filter()
function returns a sequence from those elements of iterable for which function returns True.
filter(function, iterable)
Here,
Note: filter(function, iterable)
is equivalent to [i for i in iterable if function(i)]
if function is not None and [i for i in iterable if i]
if function is None. .
>>> def func1(x):
... if x > 0:
... return x
>>> filter(func1, [-1, 0, 1])
[1]
>>> filter(None, (0, 1, True))
(1, True)