zipWith analogue in Python? zipWith analogue in Python? python python

zipWith analogue in Python?


map()

map(operator.add, [1, 2, 3], [3, 2, 1])

Although a LC with zip() is usually used.

[x + y for (x, y) in zip([1, 2, 3], [3, 2, 1])]


You can create yours, if you wish, but in Python we mostly do

list_c = [ f(a,b) for (a,b) in zip(list_a,list_b) ] 

as Python is not inherently functional. It just happens to support a few convenience idioms.


You can use map:

>>> x = [1,2,3,4]>>> y = [4,3,2,1]>>> map(lambda a, b: a**b, x, y)[1, 8, 9, 4]