Functions of filter and map

Let's talk about filter and map functions in Python.

filter

The filter built-in function will take a function and an iterable and return an iterator for those elements within the iterable for which the passed in function returns True. That statement sounds a bit confusing to read.

Example of filter()

Let’s look at an example:

Press + to interact
def less_than_ten(x):
return x < 10
my_list = [1, 2, 3, 10, 11, 12]
for item in filter(less_than_ten, my_list):
print(item)
...

Get hands-on with 1400+ tech skills courses.