49

I recently saw some Clojure or Scala (sorry I'm not familiar with them) and they did zip on a list or something like that. What is zip and where did it come from ?

Alex Miller
  • 69,183
  • 25
  • 122
  • 167
Robert Gould
  • 68,773
  • 61
  • 187
  • 272

5 Answers5

79

Zip is when you take two input sequences, and produce an output sequence in which every two elements from input sequences at the same position are combined using some function. An example in Haskell:

Input:

zipWith (+) [1, 2, 3] [4, 5, 6]

Output:

[5, 7, 9]

The above is a more generic definition; sometimes, zip specifically refers to combining elements as tuples. E.g. in Haskell again:

Input:

zip [1, 2, 3] [4, 5, 6]

Output:

[(1, 4), (2, 5), (3, 6)]

And the more generic version is called "zip with". You may consider "zip" as a special case of "zipWith":

zip xs ys = zipWith (\x y -> (xs, ys)) xs ys 
skylize
  • 1,401
  • 1
  • 9
  • 21
Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289
  • not equivalent in scala. In scala you would have to do a zip, then a map – drudru Jul 12 '09 at 08:39
  • 1
    Well then, it's not really "not equivalent", it's just that Scala doesn't have `zipWith`. Obviously, you can do either - define `zip` in terms of `zipWith`, or define `zipWith` in terms of `zip` and `map`. – Pavel Minaev Jul 12 '09 at 08:41
  • if the user is using Scala, and you give them Haskell, i'm pretty sure it won't compile, right? That is what I mean by equivalent. – drudru Jul 12 '09 at 08:44
  • I don't know Haskell but in Python and Lisp, zip can take many sequences, not just 2. – Nick Dandoulakis Jul 12 '09 at 09:07
  • 1
    It's hard to say what he's using, I mean "I recently saw some Clojure or Scala" is not exactly definite :) – Pavel Minaev Jul 12 '09 at 10:28
  • Note also that in Scheme, zip is just a special case of map (which works for multiple input lists) (map * (list 1 2 3) (list 4 5 6)) => (list 4 10 18) – Paul Hollingsworth Jul 13 '09 at 09:02
  • 2
    The zip function using zipWith is both ugly and wrong. Function passed to zipWith is it's first parameter ... and it is far nicer to write zip as zip = zipWith (,) – Martin Jonáš Nov 25 '09 at 17:06
21

zip is a common functional programming method like map or fold. You will find these functions in early lisps all the way up to ruby and python. They are designed to perform common batch operations on lists.

In this particular case, zip takes two lists and creates a new list of tuples from those lists.

for example, lets say you had a list with (1,2,3) and another with ("one","two","three") If you zip them together, you will get List((1,"one"), (2,"two"), (3,"three"))

or from the scala command line, you would get:

scala> List(1,2,3).zip(List("one","two","three"))
res2: List[(Int, java.lang.String)] = List((1,one), (2,two), (3,three))

When I first saw it in Python, without knowing functional programming, I thought it was related to the compression format. After I learned more about functional programming, I've used it more and more.

drudru
  • 4,933
  • 1
  • 19
  • 19
  • 2
    Usually when do you use zip method, or in other words, what is the typical use case for zip in real projects? – Gelin Luo Nov 10 '13 at 08:57
9

Unfortunatley I don't have enough points to even leave a comment on the top answer, but

zip xs ys = zipWith xs ys (\x y -> (xs, ys))

is wrong, it should be:

zip xs ys = zipWith (\x y -> (x,y)) xs ys

or simply:

zip = zipWith (\x y -> (x,y))
bseibold
  • 529
  • 7
  • 14
6

You could use the following code in Python:


>>> a = [1,2]
>>> b = [3,4]
>>> zip(a,b)
[(1,3),(2,4)]
Geo
  • 93,257
  • 117
  • 344
  • 520
5

Pavel's answer pretty much describes it. I'll just provide an F# example:

let x = [1;2]
let y = ["hello"; "world"]
let z = Seq.zip x y

The value of z will be a sequence containing tuples of items in the same position from the two sequences:

[(1, "hello"); (2, "world")]
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789