Python map Function
Python map function
The Python map function is a built-in function. It takes in a function and an iterable(list, tuple, etc.) as input. It applies the passed function to each item of an iteration and returns a map object.
Syntax
The general syntax for the map function is as follows:
map(function, iterable)
Example
Let’s understand the function with an example. Let’s say we want to compute the square of the first ten odd numbers.
Code listing
#Define a simple function
#www.TestingDocs.com – Python Tutorials
def square(n):
return n**2
odd_squares= map(square, range(1,20,2))
squares
#returns map object
list(odd_squares)
In this example, we have used Python’s map function instead of an explicit for loop. The output of the code is:
Output
[1, 9, 25, 49, 81, 121, 169, 225, 289, 361]
—
Python Tutorials
Python Tutorial on this website can be found at:
More information on Python is available at the official website: