Skip to content
Home » Functional Programming in Python – the map Function

Functional Programming in Python – the map Function

Spread the love

Today we’ll be talking about the map function, which is a built-in used in functional programming. In the following two articles we’ll be discussing the filter function and the reduce function, so stay tuned.

map function

Although we could do without the map, filter and reduce functions, because we can have the same functionality by using comprehensions for example, they are pretty common, so it’s good to know them.

We use the map function when we want to apply a function to each element of an iterable.

Iterables are objects like lists, tuples, ranges, dictionaries, files, and so on, which can be scanned from left to right, like for example in a for loop.

If you want to learn more about iterables and iterators, I have an article about them, so feel free to read it.

Anyway, let’s define a function and apply it to each element of the following two lists.

– a list of numbers:

>>> nums = [1, 2, 3, 4, 5]

– a list of letters:

>>> letters = ['a', 'b', 'c', 'd', 'e']

Here’s our function:

>>> def double(n):
...     return 2 * n
... 

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

And now let’s apply the function to each number in the nums list. What we will get is a collection of all the results returned from the function when it’s called on the particular items.

If you’ve read my article on iterables and iterators, you remember that maps are iterables, so if we want to view all the elements at once, we have to convert them to lists:

>>> list(map(double, nums))
[2, 4, 6, 8, 10]

And now let’s use the map function with the list of letters:

>>> list(map(double, letters))
['aa', 'bb', 'cc', 'dd', 'ee']

As mentioned before, the same can be achieved with a list comprehension. To illustrate this, let’s make a list of doubled letters using a list comprehension:

>>> [double(letter) for letter in letters]
['aa', 'bb', 'cc', 'dd', 'ee']

As expected, the output is identical.

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

You can use maps with any iterable object. Let’s see how they can be used with ranges:

>>> nums = range(1, 11)
>>> list(map(lambda x: str(10 * x) + "%", nums))
['10%', '20%', '30%', '40%', '50%', '60%', '70%', '80%', '90%', '100%']

By the way, as you can see, we can also use lambda functions with maps.

If you want to learn more about lambda expressions and anonymous functions, I have an article about that too, so don’t hesitate to give it a try.

Maps can also work with multiple sequences. Then you pass the elements of the sequences in parallel as arguments to the function. Here’s an example:

>>> def mult(a, b, c):
...     return a * b * c
... 
>>> list(map(mult, [2, 4, 6], [2, 2, 2], [10, 100, 1000]))
[40, 800, 12000]

Here a is taken from the first sequence, b from the second and c from the third.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

Here’s the video version of the article:


Spread the love
Tags:

Leave a Reply