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:
Function | This is the name of the function without parenthesis. |
Iterable | Any 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:
def cube_of_number(n):
return n ** 3
numbers = [1,2,3]
output = map(cube_of_number, numbers)
print(list(output))
Processing Return value:
The map() function returns the object of the map class. We can process this data in two ways:
- Using a for loop to iterate through the return values.
- 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.
def multiply(n):
return n * 2
numbers = [1,2,3,4,5]
output = map(multiply, numbers)
for num in output:
print(num)
2
. Using the built-in function’s like list(), tuple() to convert output to the list, tuple, etc.
def multiply(n):
return n * 2
numbers = [1,2,3,4,5]
output = map(multiply, numbers)
print(list(output))
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.
numbers = [1,2,3]
cube = list(map(lambda x : x**3, numbers))
print(cube)
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.
numbers1 = [1,2,3]
numbers2 = [4,5,6]
sum = list(map(lambda x, y : x + y, numbers1, numbers2))
print(sum)
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.
name = ["logical", "python"]
upper_name = map(str.upper, name)
print(list(upper_name))
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.
name = ["logical", "python"]
length = list(map(len, name))
print(length)
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.
numbers1 = [1,2,3]
numbers2 = [1,2,3]
power = list(map(pow, numbers1, numbers2))
print(power)