Python map() Function

Introduction

Before we start discussing python map() function, let's first understand-What is a map() function ?

Python includes a function called map() that applies a function to every element in an iterable. It enables you to develop loop-free, straightforward code.

In this tutorial, we will show you the python map() function. We will also address a few FAQs on Python map() function.

Python map() Function

The map() method is expressed as follows:

map(function, iterable, ...)

It accepts two mandatory arguments:

  • function - Function that is called for each iterable element.
  • iterable - One or more iteration-supporting objects. Iterable make up the majority of Python's built-in objects, including lists, dictionaries, and tuples.

In Python 3, the map() function produces an object with a size equal to the iterable object that was supplied. The method in Python 2 returns a list.

Let's look at an example to further illustrate how the map() method works. Consider the situation when we want to make all the strings in a list uppercase.

Using the conventional for loop is one method to do this:

directions = ["north", "east", "south", "west"]
directions_upper = []

for direction in directions:
    d = direction.upper()
    directions_upper.append(d)

print(directions_upper)
Output


['NORTH', 'EAST', 'SOUTH', 'WEST']

The code will be significantly shorter and more versatile using the map() method.

def to_upper_case(s):
    return s.upper()

directions = ["north", "east", "south", "west"]

directions_upper = map(to_upper_case, directions)

print(list(directions_upper))

We’re using the list() constructor to convert the returned map object into a list :

Output

['NORTH', 'EAST', 'SOUTH', 'WEST']

The more Pythonic solution is to use a lambda function if the callback function is straightforward:

directions = ["north", "east", "south", "west"]

directions_upper = map(lambda s: s.upper(), directions)

print(list(directions_upper))
ℹ️
An anonymous, small function is known as a lambda.

Another example of how to make a list of square numbers from 1 to 10 is shown here:

squares = map(lambda n: n*n , range(1, 11))
print(list(squares))
Output

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
ℹ️
A series of integers are generated by the range() function.

Using map() with Multiple Iterables

The map() method accepts as many iterables as you like. The callback function must take the same amount of mandatory input parameters as it does iterables.

The example that follows demonstrates how to multiply two lists element by element.

def multiply(x, y):
  return x * y

a = [1, 4, 6]
b = [2, 3, 5]

result = map(multiply, a, b)

print(list(result))
Output

[2, 12, 30]

The identical code will appear as follows when lambda functions are used:

a = [1, 4, 6]
b = [2, 3, 5]

result = map(lambda x, y: x*y, a, b)

print(list(result))

The shortest iterable is used to determine the size of the returned object when several iterables are given.

Consider the following scenario where the iterables are not of the same length:

a = [1, 4, 6]
b = [2, 3, 5, 7, 8]

result = map(lambda x, y: x*y, a, b)

print(list(result))

The excess elements (7 and 8) are ignored:

Output

[2, 12, 30]

FAQs on Python map() function

How does map() work?

map() takes a function and an iterable as arguments. It applies the function to each element of the iterable and returns an iterator yielding the results.

What is the syntax of map()?

The syntax is map(function, iterable), where function is the function to apply, and iterable is the sequence to iterate over.

Does map() modify the original iterable? 

No, map() does not modify the original iterable. It creates a new iterator with the modified elements.

What type of object does map() return?

map() returns a map object, which is an iterator containing the results of applying the function to each element of the iterable.

How can we convert the map object to a list?

To obtain a list from the map object, you can use the list() function and pass the map object as an argument.

Can map() be used with lambda functions? 

Yes, map() is often used with lambda functions, as they allow for creating small, anonymous functions directly within map().

What are the advantages of using map()? 

map() simplifies and condenses code by applying a given function to a sequence of values. It is especially useful when performing repetitive operations or transformations on data.

Conclusion

Using an iterable object and a function, the map() function of Python applies the function to each member in the iterable.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.