substituting list comprehensions for map()

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


> 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]

Nvm, it's actually easy:
  vectorsum = lambda *args: [sum(i) for i in zip(*args)]



More information about the Python-list mailing list