The filter()
function takes in two arguments: a function and an iterable. It filters the elements of the iterable by calling the given function with each of the iterable's element. It returns an iterator of only those elements that evaluate to True
.
filter(function, iterable)
Where, function
is a function to be used as a condition, it should return either True
or False
. The iterable
represents the iterable which should be filtered. Both arguments are required.
It returns an iterator object containing the elements that satisfies the given condition.
Lambda functions are often used with filter functions as the function argument, as they allow for concise filter expressions that would typically otherwise take multiple lines of code to express.
Extract odd numbers from a list of numbers: