Python Filter Exercises

Let’s check out some exercises that will help understand filter() function better.

Exercise 14-a

Using filter() function filter the list so that only negative numbers are left.


filter(f, list) can be useful. Make sure your function is a logical statement to facilitate the filtering process.

Since filter() function will return an iterator, you can use list() function to convert it to a proper Python list.

lst2 = list(filter(lambda x: x<0, lst1))
print(lst2)

Exercise 14-b

Using filter function, filter the even numbers so that only odd numbers are passed to the new list.


You can use filter(f, list). Make sure your function is a logical statement to facilitate the filtering process.

Since filter() function will return an iterator, you can use list() function to convert it to a proper Python list.

lst2 = list(filter(lambda x: x%2 == 1, lst1))