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))

Exercise 14-c

Using filter() and list() functions and .lower() method filter all the vowels in a given string.


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.

To create a lambda function with logical expression you can implement something like this:

lambda x: True if x in ….else False

 

lst = list(filter(lambda x: True if x.lower() in "aeiou" else False, str1))

Exercise 14-d

This time using filter() and list() functions filter all the positive integers in the string.


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.

To create a lambda function with logical expression you can implement something like this:

lambda x: True if x in ….else False

 

lst = list(filter(lambda x: True if x in "0123456789" else False, str1))

Exercise 14-e

Using map() and filter() functions add 2000 to the values below 8000.


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.

You can use filter() function inside your map() function:

map(f, list)

where list is a filter() function itself:

map(f1, filter(f2,list))

lst2 = list(map(lambda x: x+2000, filter(lambda x: x<8000, lst1)))

Exercise 14-f

This time swap the map() and filter() functions so that map() function is inside filter() function. Convert a number to positive if it's negative in the list. Only pass those that are converted from negative to positive 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.

You can use filter() function inside your map() function:

filter(f, list)

where list is a filter() function itself:

filter(f1, map(f2,list))

So, filter those that are negative, then convert them to positive using map() function.

lst2 = list(filter(lambda x: True if x>0 else False, map(lambda x: x*-1, lst1)))