Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 map() Function

Python3 map() Function

Python3 Built-in Functions Python3 built-in function


Description

The map() function maps a specified sequence according to a provided function.

The first argument function is called with each element of the parameter sequence, returning a new list containing the return value of each function call.

Syntax

The syntax of the map() method is as follows:


map(function, iterable, ...)

Parameters

Return Value

Returns an iterator.


Example

The following example shows how to use map():

Python3.x example

def

square

(

x

)

:        

Calculate squares

...    

return

x **

2

...

map

(

square

,

[

1

,

2

,

3

,

4

,

5

]

)

   

Calculate the square of each element in a list

<

map

object

at

0x100d3d550

   

Returns an iterator

list

(

map

(

square

,

[

1

,

2

,

3

,

4

,

5

]

)

)

 

Convert to a list using list()

[

1

,

4

,

9

,

16

,

25

]

list

(

map

(

lambda

x: x **

2

,

[

1

,

2

,

3

,

4

,

5

]

)

)

 

Use a lambda anonymous function

[

1

,

4

,

9

,

16

,

25

]

Python3 Built-in Functions Python3 built-in function