Zipping Collections

How do you loop two sequences in Clojure? IOW, What is the Clojure Python equivalent of zip(a, b) ?

EDIT:
I know how to define such a function. I was just wondering if the standard library already provides such a function. (I would be very * surprised if this is not so.)

+4
source share
3 answers

You can easily define a function like Python zip:

 (defn zip [& colls] (apply map vector colls)) 

In the case of (zip ab) it becomes (map vector ab)

+3
source

if you want the input to be lists, you can define a zip function like this

 (defn zip [m] (apply map list m)) 

and name it as follows

 (zip '((1 2 3) (4 5 6))) 

this call calls ((1 4) (2 5) (3 6))

0
source

Is it close enough?

 (seq (zipmap [1 2 3] [4 5 6])) ;=> ([3 6] [2 5] [1 4]) 
0
source

All Articles