First page Back Continue Last page Overview Graphics

Filtering of a List

The function filter(function, list) provides an elegant way to filter out those elements which are True if applied to the function.

fib = [0,1,1,2,3,5,8,13,21,34]

def is_even(x):

return x % 2 == 0

result = filter(is_even, fib)

print(result)

Exercise:

Write a call to this function using a lambda Operator

fib = [0,1,1,2,3,5,8,13,21,34]

result = filter(lambda x: x%2==0,fib)

print(result)