ScanSkill

filter

This filter() function returns a sequence from those elements of iterable for which function returns True.

Syntax

filter(function, iterable)

Here,

  • function: Required. The function that is used for creating a list.
  • iterable: Required. One or multiple(comma-separated) iterable object(s).

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. .

Examples

>>> def func1(x):
...     if x > 0:
...         return x
>>> filter(func1, [-1, 0, 1])
[1]
>>> filter(None, (0, 1, True))
(1, True)