Search⌘ K
AI Features

Functions of filter and map

Explore how to use Python's built-in filter and map functions to process iterables effectively. Learn to create functions for conditionally filtering data or applying transformations, and understand their similarities with generator expressions and list comprehensions.

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:

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

Here we create a simple function that tests if the integer we pass ...