w3resource

Python: map() function

map() function

The map() function is used to execute a specified function for each item in a iterable.

Version:

(Python 3.2.5)

Syntax:

map(function, iterable, ...)

Parameter:

Name Description Required /
Optional
function Function to be executed. Required
iterable Collection or an iterator object Required

Note: If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel.

Example: Python map() function

def calSqrt(a):
  return a*a

num = (2, 3, 4, 8)
x = map(calSqrt, num)
print(x)

# converting map object to set
numSqrt = set(x)
print(numSqrt)

Output:

<map object at 0x7ff9bbf1f588>
{16, 9, 64, 4}

Python Code Editor:

Previous: locals()
Next: max()

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.