Map Function

Download python eBook


  • map(function, iterable[, *additional_iterables])
  •, iterable[, *additional_iterables])
  • itertools.imap(function, iterable[, *additional_iterables])


functionfunction for mapping (must take as many parameters as there are iterables) (positional-only)
iterablethe function is applied to each element of the iterable (positional-only)
*additional_iterablessee iterable, but as many as you like (optional, positional-only)


Everything that can be done with map can also be done with comprehensions:

list(map(abs, [-1,-2,-3]))    # [1, 2, 3]
[abs(i) for i in [-1,-2,-3]]  # [1, 2, 3]

Though you would need zip if you have multiple iterables:

import operator
alist = [1,2,3]
list(map(operator.add, alist, alist))  # [2, 4, 6]
[i + j for i, j in zip(alist, alist)]  # [2, 4, 6]

List comprehensions are efficient and can be faster than map in many cases, so test the times of both approaches if speed is important for you.

Related Examples


1452 Contributors: 12
Sunday, January 1, 2017
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial:

Download eBook