Python Language Map Function

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Syntax

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

Parameters

ParameterDetails
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)

Remarks

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.



Got any Python Language Question?