Python filter() Function

Example

Filter the array and return a new array that only contains values equal to or greater than 22:

ages = [5, 16, 19, 22, 26, 39, 45]
def myFunc(x):
  if x < 22:
    return False
  else:
    return True
adults = filter(myFunc, ages)
for x in adults:
  print(x)

Run Instance

Definition and Usage

The filter() function returns an iterator that filters items through a function to test whether the item can be accepted.

Syntax

filter(function, iterable)

Parameter Value

Parameter Description
function Function to test each item in the iterable.
iterable iterable to be filtered.