Higher Order Functions and Map
In this tutorial, you'll learn about higher order functions in Python, including how to use them with lambda functions and the built-in map
function. Discover how these concepts can simplify your code and make it more efficient.
Let's talk about higher order functions.
A higher order function is a function that either takes one or more functions as arguments, or returns a function as its result.
Here's a simple example:
def apply_function(func, value):
return func(value)
The function apply_function
takes another function func
and a value as arguments.
It then applies the function to the value and returns the result.
Let's see how this works in practice:
def apply_function(func, value):
return func(value)
def square(x):
return x * x
print(apply_function(square, 5))
When we call apply_function(square, 5)
, we are passing the square
function as an argument to apply_function
along with the value 5
. The apply_function
then executes square(5)
, which calculates 5 * 5
and returns 25
.
While this approach works, it may not immediately seem useful. After all, we could achieve the same result by calling square(5)
directly.
Let's adjust our higher order function a bit:
def apply_function(func, values):
results = []
for value in values:
results.append(func(value))
return results
Now, instead of applying a given function to a single value, apply_function
applies the function to each item in a list.
Let's see how this works in practice:
def apply_function(func, values):
results = []
for value in values:
results.append(func(value))
return results
def square(x):
return x * x
print(apply_function(square, [1, 2, 3, 4, 5]))
Now the apply_function
applies square
to each item in [1, 2, 3, 4, 5]
.
Now, this is more useful. However, it might feel a bit complex—so let’s simplify it.
Instead of defining a separate square
function, we can use a lambda function directly within the apply_function
call:
def apply_function(func, values):
results = []
for value in values:
results.append(func(value))
return results
print(apply_function(lambda x: x * x, [1, 2, 3, 4, 5]))
What will be the output?
def apply_function(func, values):
results = []
for value in values:
results.append(func(value))
return results
print(apply_function(lambda x: x + 1, [1, 2, 3]))
We can use the apply_function
function to apply different functions to all items of a list.
This is a common use case in programming, which is why Python has a built-in function for this purpose: map
.
The map
function allows you to apply a function to every item in an iterable (like a list).
map(function, iterable)
Here's how our square example looks when using map
:
numbers = [1, 2, 3, 4, 5]
map(lambda x: x * x, numbers)
One thing to note is that the map function doesn't directly return the list of results. Instead it returns a map object.
A map object is an iterator, a concept we'll cover later.
However, you can easily transform the map object into a list using the list
function.
Let's apply this to our example:
numbers = [1, 2, 3, 4, 5]
map_object = map(lambda x: x * x, numbers)
# transform map object to list
print(list(map_object))
Or in one line of code:
numbers = [1, 2, 3, 4, 5]
print(list(map(lambda x: x * x, numbers)))
See how we drastically simplified our code using the map
function together with a lambda function? We achieved the same result using a single line of code.
What will be the output?
numbers = [1, 2, 3, 4, 5]
result = list(map(lambda x: x * 2, numbers))
print(result)
What will be the output?
numbers = [1, 2, 3, 4, 5]
result = list(map(lambda x: x + 1, numbers))
print(result)