Python map() Function

Sometimes you might face situations where you need to perform the same operation on each and every element of an iterable(list, tuple, set, etc). For example, square each number or add 10 to each number, or any such kind of operation.

The most common approach to achieve this is using a for or while loop and perform the same operation on each element. But, there is a much easier solution to this problem. Yes, the map() function.

The map() function is used in situations where we need to perform the same operation on each element of an iterable, without using any explicit for or while loop.

numbers = [1, 2, 3, 4, 5]
 
def square(num):
    return num*num
 
squares = list(map(square, numbers))
print(squares)

# Output
# [1, 4, 9, 16, 25]

map() Function Syntax

The syntax of the map() function is as follows:

map(function, iterable, ...)

map() Function Parameters

The map() function takes two parameters:

  • function – A function that performs same operation on each element of the iterable
  • iterable – The iterable on which the operation is to be performed. There can be one or more such iterables.

map() Function Return Value

The map() function returns an object of the map class. This map object contains the updated value of each item of the iterable on which we have performed the operation.

The map object can be later converted to any desired sequence like a list, tuple, set, etc.


Understanding the map() Function

Now that we have learned the basic syntax and some theoretical parts of the map() function. Let’s now understand how it actually works.

Like the for loop, the map() function iterates over each element of the iterable and one by one passes each element to the function that we have given as the first parameter.

This function then performs some operations on the current element which is passed to it and returns its new(modified) value.

Python map() Function

Example:

numbers = [1, 2, 3, 4, 5]

def square(num):
    return num*num

result = map(square, numbers)
print(result)

# Convert map object to list
resultList = list(result)
print(resultList)

Output:

<map object at 0x7fe4a17d71f0>
[1, 4, 9, 16, 25]

In the above example, the map(square, numbers) function passes each element of the numbers list to the square(num) function one by one as an argument num. The square function then calculates the square of the current num and returns it to the map() function again.

The map object is later converted to a list using the inbuilt list() function. You can also convert the map object to other sequences like: tuples, sets, etc.


map() Function with Multiple Iterables

It is also possible to pass multiple iterables to the map() function at a time. The basic functionality remains the same. The only difference is that the map() function now implicitly passes multiple elements(one from each iterable) to the function defined in the first parameter.

In the example below, the map() function adds the corresponding items of the numbers1 and numbers2 list and returns a new map object containing the corresponding items’ sum from both lists.

Example 1: When all iterables are of the same size

numbers1 = [1, 2, 3, 4, 5]
numbers2 = [10, 20, 30, 40, 50]

def add(num1, num2):
    return num1 + num2

result = map(add, numbers1, numbers2)
print(result)

# Convert map object to list
resultList = list(result)
print(resultList)

Output:

<map object at 0x7fd93bd57430>
[11, 22, 33, 44, 55]

Example 2: When all iterables are of different sizes

If all the iterables are of different sizes, the map() function iterates until the iterable with the minimum number of items has some items to be iterated. Once all these items are iterated, the map() function ignores the remaining items in other iterables.

numbers1 = [1, 2, 3]
numbers2 = [10, 20, 30, 40, 50]

def add(num1, num2):
    return num1 + num2

result = map(add, numbers1, numbers2)
print(result)

# Convert map object to list
resultList = list(result)
print(resultList)

Output:

<map object at 0x7f2b9df06250>
[11, 22, 33]

Python map() with lambda Function

Just like the normal function, a lambda function can also be used with the map function. The basic functionality remains exactly the same. The only difference is that we have to write less code.

Example:

numbers = [1, 2, 3, 4, 5]

result = map(lambda num: num*num, numbers)
print(result)

# Convert map object to list
resultList = list(result)
print(resultList)

Output:

<map object at 0x7f407e935130>
[1, 4, 9, 16, 25]

Python map() and lambda with Multiple Parameters

In previous examples, we used a custom add() function with the map() function to add corresponding items in multiple iterables passed to the map(). Now the question arises can we replace the custom add() function with a lambda expression to do the same task? The answer is yes.

Just like a normal function, the basic functionality of the lambda expression remains the same i.e. the map() function passes one item from each of the iterables to the lambda expression at a time and then moves to the next item and so on.

Example:

numbers1 = [1, 2, 3, 4, 5]
numbers2 = [10, 20, 30, 40, 50]

result = map(lambda num1,num2: num1+num2, numbers1, numbers2)
print(result)

# Convert map object to list
resultList = list(result)
print(resultList)

Output:

<map object at 0x7f3907599250>
[11, 22, 33, 44, 55]

Python map() with inbuilt Functions

Similar to a lambda function, an inbuilt Python function can also be used with the map() function. Just like the lambda function, the iterables are implicitly passed to this inbuilt function as its parameters by the map() function.

In the below example, we have used Python inbuilt pow() function with the map() function to calculate the power of each element in the numbers list. The corresponding power values are stored in the powers list.

The number and its corresponding power is implicitly passed to the pow(num, pow) function by the map().

Example:

numbers = [1, 2, 3, 4, 5]
powers  = [2, 2, 3, 2, 3]
 
result = map(pow, numbers, powers)
print(result)
 
# Convert map object to list
resultList = list(result)
print(resultList)

Ouput:

<map object at 0x7fad9080b220>
[1, 4, 27, 16, 125]