substituting list comprehensions for map()

Anh Hai Trinh anh.hai.trinh at gmail.com
Tue Nov 3 05:06:51 CET 2009


> Yes, just about any ‘map()’ operation has a corresponding list
> comprehension. (Does anyone know of a counter-example, a ‘map()’
> operation that doesn't have a correspondingly simple list
> comprehension?)

Try turning this into a list comprehension:

  vectorsum = lambda *args: map(sum, zip(*args))

  vectorsum([1,2], [3,4], [5,6])
->[9, 12]
  vectorsum([1,2], [3,4], [5,6], [7,8])
->[16, 20]

Peace,

----aht



More information about the Python-list mailing list