Python map() FunctionPython Functions

Difficulty Level:
Difficulty Level:

The map() function executes the given function for each item in an iterable.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

# Calculate squares
def get_square(x):
    return x ** 2

list1=list(map(get_square, [0,1,3,4,2]))  #Convert to a list for output.
print(list1)

Output:

[0, 1, 9, 16, 4]

Syntax

map(function, iterables)

Parameters

Name Description
function The function to execute for each item.
iterables A sequence, collection, or an iterator object.

Return Value

It returns an iterator.

Advertisement

© 2021 Pythonexample.org, all rights reserved. Privacy Policy | Contact Us