Skip to content

Functional Functions

These functions allow you to apply operations in a declarative and functional way on data collections, such as lists or tuples. They are especially useful when you need to apply a function to many elements (map), filter according to conditions (filter), combine data (zip), or generate numeric sequences (range). Their use favors a cleaner, more compact, and expressive programming style.

FunctionDescription
map()Applies a function to each element of an iterable and returns a new iterable.
filter()Filters the elements of an iterable according to a given condition.
zip()Joins several iterables into ordered pairs, such as tuples.
range()Generates a sequence of numbers, generally used in loops.
all()Returns True if all elements of an iterable are true.
any()Returns True if at least one element is true.

Below I will explain in more detail how these functions work:

Typical use: Apply a function to each element of an iterable and return a new iterable.

map.py
numbers = [1, 2, 3, 4]
squared = map(lambda x: x ** 2, numbers)
print(list(squared))

Output:

map.py
[1, 4, 9, 16]

Typical use: Filter the elements of an iterable that meet a condition.

filter.py
numbers = [10, 15, 20, 25]
greater_than_15 = filter(lambda x: x > 15, numbers)
print(list(greater_than_15))

Output:

filter.py
[20, 25]

Typical use: Combine multiple iterables into ordered pairs (tuples).

zip.py
names = ['Ana', 'Luis', 'Sofía']
ages = [28, 34, 25]
pairs = zip(names, ages)
print(list(pairs))

Output:

zip.py
[('Ana', 28), ('Luis', 34), ('Sofía', 25)]

Typical use: Generate a numeric sequence, commonly used in loops.

range.py
for number in range(0, 10, 2): # From 0 to 8 in steps of 2
print(number, end='')

Output:

range.py
02468

Typical use: Check if all elements of an iterable are true.

all.py
values = [True, 1, 5 > 2]
print(all(values))

Output:

all.py
True

Typical use: Check if at least one of the elements of an iterable is true.

any.py
values = [0, '', None, 7]
print(any(values))

Output:

any.py
True