Mapping

Contents
  • map
  • With more than one iterator
  • starmap

If you’ve used only one of Python’s iterator functions, there’s a good chance it’s this one.map in Python

map is essential because it’s the functor operation for lists:The notion of a functor is a small thing, but it provides a surprising depth of useful ideas to think about. We go into functors in depth in Functortown: A Map of the Territory. It operates over the content of the list but not the structure of the list. This implies, among other things, that the list that map returns will always have the same number of elements as the input list.

map

>>> xs = [1, 2, 3]

>>> it = map(lambda x: x * 10, xs)

>>> list(it)
[10, 20, 30]

Sign up for access to the full page, plus the complete archive and all the latest content.