Python map() Function

The map() function executes a given function to each element of an iterable (such as lists, tuples, etc.).

Example

numbers = [1,2,3,4]

# returns the square of a number
def square(number):
  return number * number

# apply square() to each item of the numbers list squared_numbers = map(square, numbers)
# converting to list for printing result = list(squared_numbers) print(result) # Output: [1,4,9,16]

map() Syntax

map(function, iterables)

map() Arguments

The map() function takes two arguments:

  • function - a function that is applied to each element of an iterable.
  • iterables - iterables such as lists, tuples, etc.

Note: We can pass more than one iterable to the map() function.


map() Return Value

The map() function returns a map object, which can be easily converted to lists, tuples, etc.


Example: Working of map()

def square(n):
    return n*n

numbers = (1, 2, 3, 4)
result = map(square, numbers)
print(result) # converting the map object to set result = set(result) print(result)

Output

<map object at 0x7f722da129e8>
{16, 1, 4, 9}

In the above example, we have defined a tuple named numbers with 4 elements. Notice the line

result = map(square, numbers)

Here, the map() function squares each element of the tuple using the square function. The initial output <map object at 0x7f722da129e8> represents a map object

Finally, we convert the map object to a set and obtain the squared values of each element in tuple.

Note: The output is not in order because sets in Python are unordered collections and do not preserve the original sequence of elements.


map() with Lambda

In a map() function, we can also use a lambda function instead of a regular function. For example,

numbers = (1, 2, 3, 4)
result = map(lambda x: x*x, numbers)
print(result) # convert to set and print it print(set(result))

Output

<map object at 0x7fc834e22170>
{16, 1, 4, 9}

In the above example, we have directly used the lambda function to perform the square of each element in the tuple.

Note: Use of lambda() function makes the code concise and easier to read.


Add Multiple Lists using map() and lambda

We can use map() and lambda to add multiple lists in Python. For example,

num1 = [1, 2, 3]
num2 = [10, 20, 40]

# add corresponding items from the num1 and num2 lists
result = map(lambda n1, n2: n1+n2, num1, num2)
print(list(result))

Output

[11, 22, 43]

In the above example, we have passed two lists num1 and num2 to the map() function. Notice the line,

result = map(lambda n1, n2: n1+n2, num1, num2)

Here, the lambda function is used within map() to add the corresponding elements of the both lists.


String Modification using map()

We can use map() function to modify the string. For example,

# list of actions
actions=['eat', 'sleep','read']
# convert each string into list of individual characters 
result= list(map(list,actions))
print(result)

Output

[['e', 'a', 't'], ['s', 'l', 'e', 'e', 'p'], ['r', 'e', 'a', 'd']]

In the above example, we have used list() around map() to convert each string to a list of individual characters.

Also Read:

Video: Python map() and filter()

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges