filter(function, iterable)
Filters elements of an object using a specified function.
Parameters
The filter() function takes two arguments:
function- a filtering function that accepts an element of the object to be filtered. If the function returnsFalse, that element is excluded. IfNoneis passed, it acts as an identity function (lambda *args: args), filtering out elements evaluated asFalse.iterable- the object whose elements are filtered. It can be a sequence, an object supporting iteration, or an iterator. If the object is a string or a tuple, the result matches the type; otherwise, a list is returned.
Return Value
- The
filter()function returns an iterator. Note: You can convert iterators into sequences like lists, tuples, or strings.
Examples
filter(func, iterable)
# Python 3 equivalent — generators
# if func is not None
(item for item in iterable if function(item))
# if func is None
(item for item in iterable if item)