In this article we will discuss how to transform the contents of a iterable sequence using map(). Also we will discuss how to use map() function with lambda functions and how to transform a dictionary using map() too.

Python’s map() Function

Python provides a function map() to transform the contents of given iterable sequence based on logic provided by us i.e.

It accepts following arguments:

  • sequence : An iterable sequence
  • callback : A function that accepts an element as an argument and returns a new element based on the argument.

Returns :

  • Returns a new sequence whose size is equal to passed sequence.

How does map() function works ?

It iterates over all the elements in given sequence. While iterating the sequence it calls the given callback() function on each element and then stores the returned value in a new sequence. In the end return this new sequence of transformed elements.

Now let’s see some examples of map() function,

Use map() function with list of strings and a global function

Suppose we have a list of strings i.e.

Now let’s convert each string element in the list using map() function i.e.

Output:

As arguments, map() functions accepts our original list of string and a function reverseStr(). It iterates over all the string elements in our list
and calls reverseStr() for each string element. Also stores the value returned by reverseStr() in a sequence. In the end it returns the new  sequence with reversed strings.

We can also use the map() function to convert a list of strings to a list of numbers representing the length of each string element i.e.

Output:

It iterates over the list of string and apply len() function on each string element. Then stores the length returned by len() in a new sequence for every element. Then in last returns the new sequence of int i.e length of each string element.

Use map() function with lambda function

In the map() function along with iterable sequence we can also the lambda function. Let’s use lambda function to reverse each string in the list like we did above using global function,

Output:

It iterates over the list of string and apply lambda function on each string element. Then stores the value returned by lambda function to a new sequence for each element. Then in last returns the new sequence of reversed string elements.

Convert a string to other format using map() function in Python

We can also convert a string to another format using map() function because string is also an iterable sequence of characters. Let’s see how to transform strings using map(),

Suppose we have String i.e.

Now let’s transform this string by increment each character by 1 in it’s ascii value i.e. convert ‘a’ to ‘b’ and ‘e’ to ‘f’ etc,

Output:

map() function iterated over each character in the given string and applied the given lambda function on each character to increment it’s ASCII value by 1. In the end it returned a new string with modified content.

Passing multiple arguments to map() function in Python

The map() function, along with a function as argument can also pass multiple sequence like lists as arguments. Let’s see how to pass 2 lists in
map() function and get a joined list based on them.

Suppose we have two lists i.e.

Now we want to join elements from list1 to list2 and create a new list of same size from these joined lists i.e. new lists should be like this,

To do that we can pass both the lists and a lambda function in map() i.e.

Output:

Here passed lambda function accepts two arguments and returns a new string by join the passed arguments. map() function iterates over both the lists simultaneously and pass their each element to lambda function. Then Values returned by lambda function are stored in a new list and returned in the end.

Both the lists passed to map() function should be of same size otherwise it will through error.

Another example,

Suppose we have a lists of numbers i.e.

Now let’s multiply each element in lists by 2 and get a new lists of updated values using map() i.e.

Output:

Using map() function to transform Dictionaries in Python

Suppose we have a dictionary i.e.

Now we want to transform the values in dictionary by appending ‘_’ at the end of each value i.e.

Output:

map() function iterated over all the items in dictionary and then applied passed lambda function on each item. Which in turn updated the value of each item and returns a copy of original dictionary with updated contents.

Complete example is as follows:

Output:

Join LinkedIn Group of Python Professional Developers who wish to expand their network and share ideas.

You can also follow us On Twitter :

Click Here to Subscribe for more Articles / Tutorials like this.