<aside> 👨🏼‍💻 The following built-in functions allows you to easily process iterables. I’ll provide some examples.

</aside>

Support my work buying me a coffee ☕

Martí Grau is R&D and IA engineer

filter()

It filters a given iterable with the help of a function

filter(function, iterable)

# Example
def is_positive(n):
	return n > 0

list(filter(is_positive, numbers))
>>>[1, 2]

map()

Similar to filter(), but applies a function to all elements

map(function, iterable[iterable1, ..., iterableN]))

# Example
numbers = [1, 3, 10, 45, 6, 50]

def is_even(number):
	return number % 2 == 0

even_numbers = list(map(is_even, numbers))
print(even_numbers)
>>> [10, 6, 50]

lambda or ‘anonymus function’

Is a function without a name

lambda x: a+b

x = lambda a : a + 10
print(x(5))
>>> 15

Partial

Partial functions allow us to fix a certain number of arguments of a function and generate a new function.

from functools import partial
def sum(x,y):
    return x + y
partial_sum = partial(sum,7)
print(partial_sum(13))