Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zipWith analogue in Python?

Tags:

python

haskell

What is the analogue of Haskell's zipWith function in Python?

zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] 
like image 975
Yrogirg Avatar asked Mar 19 '12 07:03

Yrogirg


People also ask

Why do you use the zip () method in python?

Python's zip() function creates an iterator that will aggregate elements from two or more iterables. You can use the resulting iterator to quickly and consistently solve common programming problems, like creating dictionaries.

Can you zip more than two lists Python?

The Python zip() function makes it easy to also zip more than two lists. This works exactly like you'd expect, meaning you just only need to pass in the lists as different arguments.

How do you zip two tuples in Python?

If multiple iterables are passed, zip() returns an iterator of tuples with each tuple having elements from all the iterables. Suppose, two iterables are passed to zip() ; one iterable containing three and other containing five elements. Then, the returned iterator will contain three tuples.

How do you zip multiple lists in Python?

To zip multiple lists, you can just do zip(list1, list2, list3) , etc.


1 Answers

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])] 
like image 158
Ignacio Vazquez-Abrams Avatar answered Sep 22 '22 15:09

Ignacio Vazquez-Abrams