Python map()


Python map() function

The Python map() function applies the specified function to each item of an iterable like a list, tuple, etc.

After performing the specified operation, it returns the new iterator/sequence as the result.

Syntax:

map(function, iterable)

Parameters:

FunctionThis is the name of the function without parenthesis.
IterableAny sequence like list, tuple, etc.

Return:

It returns the object of the map() class. This return value can be processed in two ways:

  • Converting to sequence (example, convert to list using the list() function.)
  • Iterating using the loop.

Example:

In [1]:
def cube_of_number(n):
    return n ** 3
In [2]:
numbers = [1,2,3]

output = map(cube_of_number, numbers)
print(list(output))
[1, 8, 27]

Processing Return value:

The map() function returns the object of the map class. We can process this data in two ways:

  1. Using a for loop to iterate through the return values.
  2. Using list(), tuple() functions to convert output to the list, tuple, etc.

1. Using a for loop to iterate through the return values. The loop will iterate over all the items and process each item one by one.

In [1]:
def multiply(n):
    return n * 2
In [2]:
numbers = [1,2,3,4,5]

output = map(multiply, numbers)

for num in output:
    print(num)
2
4
6
8
10

2. Using the built-in function’s like list(), tuple() to convert output to the list, tuple, etc.

In [1]:
def multiply(n):
    return n * 2
In [2]:
numbers = [1,2,3,4,5]

output = map(multiply, numbers)
print(list(output))
[2, 4, 6, 8, 10]

map() with lambda

The map() function can be used with the lambda function. We need not write the separate function to find the cube of the number, lambda will do the job. This is actually the efficient use of the lambda and the map function to write the more clean and pythonic code.

In [1]:
numbers = [1,2,3]

cube = list(map(lambda x : x**3, numbers))

print(cube)
[1, 8, 27]

Multiple iterators using map

Multiple iterators can be passed to the map function, each separated by a comma. These multiple items can be processed using the lambda function or can be passed as the parameters to the separate functions.

In [1]:
numbers1 = [1,2,3]
numbers2 = [4,5,6]


sum = list(map(lambda x, y : x + y, numbers1, numbers2))

print(sum)
[5, 7, 9]

Map with Strings

The built-in string functions can be used inside the map function to process the strings. In the below example, a list having strings “logical” and “python” is converted to upper case using the built-in function str.upper. Please note that no parentheses are used after the function name.

In [1]:
name = ["logical", "python"]

upper_name = map(str.upper, name)

print(list(upper_name))
['LOGICAL', 'PYTHON']

Using built-in functions

The map() function can be used with the built-in functions like len(). Note: Here also, no parenthesis is used here after the function name and no value is passed to the function. It is done automatically by the python.

In [1]:
name = ["logical", "python"]

length = list(map(len, name))

print(length)
[7, 6]

Using built-in functions with multiple iterables

The built-in functions that require two arguments, for example, power – pow() can be used with the map() function. No parenthesis is used here, and arguments to the function are passed automatically. We need not specify the values of arguments. It is done automatically by the python.

In [1]:
numbers1 = [1,2,3]
numbers2 = [1,2,3]


power = list(map(pow,  numbers1, numbers2))

print(power)
[1, 4, 27]

References

  1. The map() function in Python.
  2. How To Use the Python Map Function.