Python map() function

Published February 16, 2022

The map() function is a built-in function that lets you work with and change iterable objects without using the explicit for loop. When using map(), you apply this method to each item in the specified iterable, which may be a tuple, a list, etc., and obtain an iterator or map object as a result.
A map() loops over an iterable and applies a function to each item, returning a new iterator containing the modified objects.

Syntax
The map() function is implemented using the simple syntax shown below.

map(function, iterable)

where:
function: A function that will be called for each iterable element.
iterable: the iterable to be mapped

Let's look at an example to see how map() works in more detail:

Example

def sum(a):
    return a + a
x = (6,7,9,8,4,5)
b = map(sum, x)
print(list(b))

 

Output

[12, 14, 18, 16, 8, 10]

 

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

336 Views