filter() function

Filter() is a built-in python function. It gets a list and a function that gets only one parameter.
Filter iterates over the items. If the result of the function is True, the item is added to the result list. If the result is False the items is filtered out.

Watch the example:

#filter my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] def is_odd(x): return(x % 2) new_filter = filter(is_odd, my_list) print(list(new_filter)) print(list(filter(lambda a: a % 2, range(20))))

Exercise

  1. Create a list of numbers.
  2. Write a filter function (with a lambda) and print out the numbers only if they are even.

#Write your code here: