The map Function

map(func, s) applies a function to each element of a sequence

     a = [1,2,3,4,5,6]
     def foo(x):
         return 3*x
     
     b = map(foo,a)     # b = [3,6,9,12,15,18]
  • Alternatively...
     b = map(lambda x: 3*x, a)  # b = [3,6,9,12,15,18]

Special cases

  • map can be applied to multiple lists.
     b = map(func, s1, s2, ... sn)
  • In this case, func must take n arguments.
  • If the function is None, the identity function is assumed.
<<< O'Reilly OSCON 2000, Introduction to Python, Slide 86
July 17, 2000, beazley@cs.uchicago.edu
>>>