Kodeclik Blog

Python Map Function

Python’s map function is easy to motivate. Let us assume you have a list of numbers:
This gives the output:

Using Python’s map function to square the elements in a list

Let us suppose you want to square each element and return a new list. We usually do it with a for loop.
This produces the output:
An easier way to do it is using the map() function. With map we view the function as a “hammer” and apply it uniformly to every element of the list.
In our case, the hammer is the square function. map() takes as input the hammer function and the list that you want to apply it to, like so:
See how elegant this notation is? The functionality of squaring a number is factored out in the function square(x). The map function applies it uniformly to each element of the list.
Let us try to print the list we just created:
This gives:
Woah! What does this mean? The output above says that squaredlist is a map object (an iterable) but it is computed lazily, only on demand, when you really need it. For this purpose, we use the list() constructor:
This produces:

Using Python map to convert temperatures

Because the map function factorizes out the operation from the iterable it is very easy to adapt the above code to do a different operation. For instance, suppose I want to take a list of temperatures in fahrenheit and convert them to celsius:
This produces:
You can use a formatted print to restrict each output to have a max of 2 digits after the decimal (for instance).

Using lambda expressions in place of defined functions

If you do not desire or need to define a function for use in map(), you can substitute a lambda function that defines the function without naming it.
This produces the same output as before:

Applying map() to multiple iterables

The map() function is not restricted to using only one list as input. If your function can be defined as a systematic iteration over multiple lists you can use map for the same purpose. For instance assume you have a list of prices of fruits and another list of amount of fruit:
This produces the output:
Note that in the above code we are using the map() function with two lists and the corresponding elements of the lists are multiplied to yield the prices.

Using map() with strings

Because strings are also iterables in Python, we can apply map() over a string and it will consistently apply your given function on each element of the string. For instance, we can use map() to take a string and encode it into a secret representation by simply incrementing each character's ascii value by 1:
This outputs:
Note that ‘m’ has been transformed to ‘n’, ‘a’ has been transformed to ‘b’, and so on.
The problem with this program is that the result is not a string anymore. We can use Python’s join() function to accomplish convert the resulting list back to a function:
The output is now:

Using Python map() with list()

A cute use of map() happens when list() is the function being mapped over a list. Note that list() takes an iterable and constructs a list out of it. And recall that strings are also iterables. So you can take a string and make each element of the string to form a list.
Take the following piece of code:
This seems a bit complicated but let us take it step by step from the innermost construct. list() is a function that takes an iterable and constructs a list out of its elements. The use of map() above allows us to take each element of the string and make it into a list (a singleton list containing only one element). We then use another list outside to be able to print and view the results:
So this above code snippet takes a string and makes it a list of lists. Of course this example does not really make use of the power of the map() function because our list has only one element. Here is an example with a longer input list:
This gives, as expected:

Creating license plate suggestions

Let us write a program to create license plate suggestions. License plate names need to be short but still recognizable. One way to create short strings is to remove the vowels in your string. Here’s how we might typically write such a function and use it:
The output will be:
Note that we haven’t used map() in this example. Let us remedy that. We will first use a function to remove a single vowel (character) and then apply it over a string iterable:
This outputs:
The problem with this is that the result is not a string anymore. We will use Python’s join() function as before to accomplish this:
This outputs:
So what have we learnt in this blog post? Python’s map() function is a very handy approach to uniformly applying a procedure/function on an iterable (e.g., a list or a string) without explicitly using a for loop. A very closely related Python function is the reduce() function.
If you liked the functional programming style described in this blog, checkout how to generate all permutations of a given string in Python.
Interested in more things Python? See our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2023. All rights reserved.