Map in Python is a function that works as an iterator to return a result after applying a function to every item of an iterable (tuple, lists, etc.). It is used when you want to apply a single transformation function to all the iterable elements. The iterable and function are passed as arguments to the map in Python.

Want a Top Software Development Job? Start Here!

Full Stack Development-MEANExplore Program
Want a Top Software Development Job? Start Here!

Syntax of Map in Python

The syntax of the Python map() function is:

map(function, iterables)

In the above syntax:

  • function: It is the transformation function through which all the items of the iterable will be passed.
  • iterables: It is the iterable (sequence, collection like list or tuple) that you want to map.

Let’s look at an example to understand the syntax of the map in Python better. The code below creates a list of numbers and then uses the Python map() function to multiply each item of the list with itself.

# Defining a function

def mul(i):

    return i * i

# Using the map function

x = map(mul, (3, 5, 7, 11, 13))

print (x)




As you can see in the code above, the first print function printed the map object as it is. Thus, you have to convert the map object to a list, set, or another iterable item to make it readable.

Workings of the Python Map() Function 

The map in Python takes a function and an iterable/iterables. It loops over each item of an iterable and applies the transformation function to it. Then, it returns a map object that stores the value of the transformed item. The input function can be any callable function, including built-in functions, lambda functions, user-defined functions, classes, and methods. Now, calculate the same multiplication as you did in the previous example, but this time with both the for loop and the map functions separately, to understand how the map() function works.

Example: Using For Loop

num = [3, 5, 7, 11, 13]

mul = []

for n in num:

    mul.append(n ** 2)

print (mul)



Example: Using the Python Map() Function

def mul(i):

    return i * i

num = (3, 5, 7, 11, 13)

resu = map(mul, num)


# making the map object readable

mul_output = list(resu)




As you can see, the map() function iterates through the iterable, just like the for loop. Once the iteration is complete, it returns the map object. You can then convert the map object to a list and print it. You would have also noticed that this time while using the map in Python, you defined the iterable separately and then passed it to the map() function. Thus, you can either define the iterable within the map() function or separately.

Want a Top Software Development Job? Start Here!

Full Stack Development-MEANExplore Program
Want a Top Software Development Job? Start Here!

How Map in Python is Used With Built-In Functions

You can also use the Python map() function with built-in functions that are pre-defined. Let’s see this in action.

Example: Using Map() With Len()

In this code, you must use the Python len() function along with map() to find the length of some words.

example = ["Welcome", "to", "Simplilearn"]

x = list(map(len, example))




Example: Using Map in Python With Math.Sqrt()

In the program below, you will first import the math library to use the math.sqrt() function with map in Python.

import math

num = [9, 36, 49, 81, 121]

x = list(map(math.sqrt, num))




Similar to the above examples, you can use any built-in functions with the map in Python.

Using Map in Python with Lambda Functions

One of the most common use cases of the map function you might encounter is with the lambda functions. Lambda functions are anonymous functions, i.e., without any name. The lambda function allows you to define the function right inside the map() function. Let’s look at an example to see how the Python map() function is used with the lambda functions.

Example: Using Map() with Lambda Functions

In the code mentioned below, you will use a lambda function to double the numbers in the iterable using the map function.

num = (6, 9, 21, 44)

resu = map(lambda i: i + i, num)




Want a Top Software Development Job? Start Here!

Full Stack Development-MEANExplore Program
Want a Top Software Development Job? Start Here!

How Map in Python is Used as an Iterator

You can use the Python map() function with a String. While using the map() with a String, the latter will act like an array. You can then use the map() function as an iterator to iterate through all the string characters. Let’s look at an example to see it in action.

Example: Using Map in Python as an Iterator

def upper(n):

    return n.upper()

stri = "This is Simplilearn"

upd_list = map(upper, stri)





How Map() is Used With Tuple

In this code, you will take a tuple containing some string values. You will then define a function to convert the strings to uppercase. Lastly, you must use the map in Python to use the tuple and the function, to convert the string values to uppercase.

Example: Map() with Tuple

def example(s):

    return s.upper()

tuple_exm = ('this','is','map','in','python', 'article')

upd_tup = map(example, tuple_exm)





Using Map in Python with Dictionary

A dictionary in Python is another collection type that stores key: value pairs. You can define a dictionary using curly brackets. In the example below, you will use a dictionary of car names and append the names with a ‘_’ in the end by using the map() function. You can see that a lambda function was used for this example.

Example: Using Dictionary with Map()

car_dict ={'a': 'Mercedes-Benz', 'b': 'BMW', 'c': 'Ferrari', 'd': 'Lamborghini', 'e': 'Jeep'}

# adding an '_' to the end of each value

car_dict = dict(map(lambda x: (x[0], x[1] + '_'), car_dict.items() ))

print('The modified dictionary is : ')




Want a Top Software Development Job? Start Here!

Full Stack Development-MEANExplore Program
Want a Top Software Development Job? Start Here!

How to Use Map() With Set

Similar to other iterables, you can also use a set with the Python map() function. In the example below, you must define a set with some numbers and print each value’s remainder when divided by 3.

Example: Using Set and Map()

def example(i):

    return i%3

set_exm = {33, 102, 62, 96, 44, 28, 227}

upd_itms = map(example, set_exm)





Summing It Up

In this article, you learned about the map in Python. You have seen some examples to see how it operates with different functions and iterables. Some of the key points to remember are that the Python map() function is used to apply a transformation function to an entire iterable. Also, you can pass multiple iterables to a single map() function. If you want to learn more about the iterable, map, or other basic Python programming concepts, you can opt for Simplilearn’s Caltech Coding Bootcamp.

Have any questions for us? Leave them in the comments section and our experts will get back to you on the same, ASAP!

About the Author

Ravikiran A SRavikiran A S

Ravikiran A S works with Simplilearn as a Research Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.