Often you may face cases where you need to execute the same procedure on all the objects of an iterable input to generate a new iterable. Python’s map() is an integrated method that enables all the objects to be interpreted and translated into an iterable instead of an explicit loop, usually referred to as mapping. Using a Python for loop is the simplest but using the map, you can also solve this issue without the need for an explicit loop(). When you’re about to implement a transformation method to each object in an iterable, map() helps translate them into a fresh iterable. One of the methods which are promoting a functional programming type in Python is a map(). In this guide, you will learn about how the map() method works with different object types.

Syntax:

Map(function, iterable, …)

Function: The method that should be performed for each object.

Iterable: A series, array, or entity with an iterator. You can include as many iterables as you want; just ensure that each iterable has one argument in the method.

Python Map() on Tuple:

Example 01:

Here is an example to concatenate two tuples having string values. We have a function ‘fruit’ taking two arguments. Value at the first index of the first tuple will be concatenated with value at the first index of the second tuple by the ‘fruit’ function and so on for other values as well. The concatenated value will be returned to the map function to do mapping and save to one variable ‘x’. After that, variable ‘x’ will be printed in map format as well as in list format.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-87.png" data-lazy- height="91" src="data:image/svg xml,” width=”594″>

The first print statement will print the mapped value residing in variable ‘x’, and the other will output it as a list after conversion.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-88.png" data-lazy- height="56" src="data:image/svg xml,” width=”610″>

Example 02:

There is a list of items being squared. It will then be mapped and converted into a set type to print it in a set format.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-89.png" data-lazy- height="124" src="data:image/svg xml,” width=”608″>

You can see a map output and set format output below.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-90.png" data-lazy- height="55" src="data:image/svg xml,” width=”615″>

Example 03:

Using the same example in the case of an addition, while converting mapped tuple into the list to print it in a sequence is shown below.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-91.png" data-lazy- height="125" src="data:image/svg xml,” width=”601″>

You can see a mapped output as well as a list format of mapped values.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-92.png" data-lazy- height="50" src="data:image/svg xml,” width=”607″>

Example 04:

We have a tuple passed into the function ‘case’ to be converted into the upper case while the ‘iterator’ method displays white-space iterator items.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-93.png" data-lazy- height="167" src="data:image/svg xml,” width=”619″>

The resulting output for the tuple is given.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-94.png" data-lazy- height="53" src="data:image/svg xml,” width=”607″>

Python Map() on List:

Example 01:

You can try the map function with the list type variable as same as you did for a tuple. So, using the same example in the case of list type, we are squaring the list elements and, after mapping, save them into a variable ‘New’. Print out the mapped variable and, after that, convert the mapped result into list format.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-95.png" data-lazy- height="98" src="data:image/svg xml,” width=”604″>

Below is the mapped list’s output and its conversion into the list after the mapping has been applied.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-96.png" data-lazy- height="48" src="data:image/svg xml,” width=”608″>

Example 02:

You can also use the same concept while using the round function to convert decimal numbers into accurate numbers.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-97.png" data-lazy- height="64" src="data:image/svg xml,” width=”608″>

Here is a result of the list being mapped and rounded off.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-98.png" data-lazy- height="48" src="data:image/svg xml,” width=”607″>

Example 03:

We can also use more than two lists in an argument to a map function as below.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-99.png" data-lazy- height="138" src="data:image/svg xml,” width=”617″>

Output for this example is given below.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-100.png" data-lazy- height="50" src="data:image/svg xml,” width=”608″>

Example 04:

Now let’s have a list of some strings. We firstly convert each element in it to a list then mapped it. After that, the mapped result will be ordered in list form to be saved in the variable ‘test’ to be printed out.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-101.png" data-lazy- height="65" src="data:image/svg xml,” width=”608″>

The result is shown in the attached photo.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-102.png" data-lazy- height="37" src="data:image/svg xml,” width=”609″>

Example 05:

Using the same old example in the case of list converted into the upper case while using the ‘iterator’ method.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-103.png" data-lazy- height="166" src="data:image/svg xml,” width=”605″>

Here is the output for the list type variable.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-104.png" data-lazy- height="51" src="data:image/svg xml,” width=”605″>

Python Map() on String:

Example 01:

We are using the case function to convert a string into the upper case while performing the mapping. After that, we will find the type of a new variable and convert it into a list type to show it in a sequence.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-105.png" data-lazy- height="77" src="data:image/svg xml,” width=”614″>

You can see an output showing the variable type and list the mapped variable.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-106.png" data-lazy- height="59" src="data:image/svg xml,” width=”609″>

Example 02:

Here is an example of a new string while the iterator method has been used.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-107.png" data-lazy- height="49" src="data:image/svg xml,” width=”600″>

You can see the output generated by the iterator method.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-108.png" data-lazy- height="48" src="data:image/svg xml,” width=”607″>

Python Map() on Dictionary:

Example 01:

We have a dictionary with some numbers passed to the Map function to multiply each element with 10 and then apply it. After that, the variable ‘final’ has been printed.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-109.png" data-lazy- height="91" src="data:image/svg xml,” width=”609″>

Multiplied and mapped dictionary is printed out in mapped and list format.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-110.png" data-lazy- height="50" src="data:image/svg xml,” width=”603″>

Python Map() on Lambda:

Example 01:

Within Python, to create anonymous methods, lambda functions are used. To describe regular functions, you would be using the lambda term just like you use def. Here is an example of a simple tuple named ‘numbers’. Lambda expression is used within the map function to calculate some value, while the result has been saved and printed.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-111.png" data-lazy- height="48" src="data:image/svg xml,” width=”610″>

Calculated output using lambda expression within map function is displayed as a list.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-112.png" data-lazy- height="38" src="data:image/svg xml,” width=”609″>

Example 02:

The map works the same for multiple arguments in lambda. Below is an example of two list having elements been added.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-113.png" data-lazy- height="60" src="data:image/svg xml,” width=”604″>

The addition of two list elements is shown.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-114.png600a2fc751fcc.jpg" data-lazy- height="39" src="data:image/svg xml,” width=”610″>

Example 03:

The map function works the same on Lambda while taking arguments of different sizes.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-115.png600a2fc7c8130.jpg" data-lazy- height="62" src="data:image/svg xml,” width=”598″>

Example 04:

The map method adds no difference while used with two different data types.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-116.png600a2fc849579.jpg" data-lazy- height="32" src="data:image/svg xml,” width=”603″>

Python Map() on None:

Example 01:

Let’s have a look at what occurs when we pass the method as None to the iterator variable.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-117.png600a2fc8b9471.jpg" data-lazy- height="66" src="data:image/svg xml,” width=”608″>

It will result in the exception of TypeError.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/word-image-118.png600a2fc937218.jpg" data-lazy- height="123" src="data:image/svg xml,” width=”619″>

Conclusion:

You could use map() efficiently in your programs with this guide information or, similarly, use list comprehensions or generator statements to make the development process more Pythonic and understandable. The map() function applies a particular function to each object of an iterable (list, tuple, etc.) and returns a list of the outcomes.

About the author

<img alt="Aqsa Yasin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Author-Image-150×150.jpg600a2fc9aec0a.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.