Transpose a list of lists in Clojure

2012-07-14

Python's zip(*lists) becomes (apply map vector lists) in Clojure (or mapv if you like it).

Python:

>>> zip(*[[1,'a'],[2,'b']])
[(1, 2), ('a', 'b')]

Clojure:

user> (apply mapv vector [[1 "a"] [2 "b"]])
[[1 2] ["a" "b"]]

In Haskell just:

ghci> import Data.List
ghci> Data.List.transpose [[1,2],[3,4]]
[[1,3],[2,4]]