Easy Tutorial
❮ Ref Math Factorial Python Module ❯

Python3 map() Function

Python3 Built-in Functions


Description

The map() function applies a given function to all items in an input iterable (list, tuple etc.) and returns a map object (an iterator).

The first argument function is called with each element of the iterable, and a new list is returned containing the values returned by the function calls.

Syntax

Here is the syntax for the map() method:

map(function, iterable, ...)

Parameters

Return Value

Returns an iterator.


Examples

The following examples demonstrate the use of map():

Python3.x Examples

>>> def square(x):          # Function to compute the square of a number
...     return x ** 2
... 
>>> map(square, [1,2,3,4,5])    # Compute the square of each element in the list
<map object at 0x100d3d550>     # Returns an iterator
>>> list(map(square, [1,2,3,4,5]))   # Convert to a list
[1, 4, 9, 16, 25]
>>> list(map(lambda x: x ** 2, [1, 2, 3, 4, 5]))   # Using a lambda function
[1, 4, 9, 16, 25]
>>>

Python3 Built-in Functions

❮ Ref Math Factorial Python Module ❯