Function Details: filter

Description


Constructs an iterator from elements of iterable for which function returns True.


Extended Description


The filter() function creates an iterator from elements of the input iterable for which the given function returns True. If the function is None, the identity function is assumed, which means filter() will return the elements that are truthy. The returned filter object is itself an iterator and can be used in for loops or converted to other sequence types.


Exceptions


  • TypeError: If the arguments are not callable and iterable, respectively

Read More about filter from Python Documentation

Function Signature


filter(function: Optional[Callable], iterable: Iterable) -> filter

Module: builtins

Class: filter

Parameters


function: A function that tests each element in the iterable. If None, returns elements that are truthy. iterable: The iterable to be filtered.


Parameter List


  • function: Optional[Callable]
  • iterable: Iterable

Return


Returns a filter object, which is an iterator of the elements for which the function returns True.


Return Type


filter

Output

Explanation

This example uses filter() with a lambda function to get even numbers from a list.