In computer science, zipping is a function which maps a tuple of sequences into a sequence of tuples. This name zip derives from the action of a zipper in that it interleaves two formerly disjoint sequences. The inverse function is unzip.

Example

Given the three words cat, fish and be where |cat| is 3, |fish| is 4 and |be| is 2. Let ℓ {\displaystyle \ell } denote the length of the longest word which is fish; ℓ = 4 {\displaystyle \ell =4}. The zip of cat, fish, be is then 4 tuples of elements:

( c , f , b ) ( a , i , e ) ( t , s , # ) ( # , h , # ) {\displaystyle (c,f,b)(a,i,e)(t,s,\#)(\#,h,\#)}

where # is a symbol not in the original alphabet. In Haskell this truncates to the shortest sequence ℓ _ {\displaystyle {\underline {\ell }}}, where ℓ _ = 2 {\displaystyle {\underline {\ell }}=2}:

Definition

Let Σ be an alphabet, # a symbol not in Σ.

Let x1x2... x|x|, y1y2... y|y|, z1z2... z|z|, ... be n words (i.e. finite sequences) of elements of Σ. Let ℓ {\displaystyle \ell } denote the length of the longest word, i.e. the maximum of |x|, |y|, |z|, ... .

The zip of these words is a finite sequence of n-tuples of elements of (Σ ∪ {#}), i.e. an element of ( ( Σ ∪ { # } ) n ) ∗ {\displaystyle ((\Sigma \cup \{\#\})^{n})^{*}}:

( x 1 , y 1 , … ) ( x 2 , y 2 , … ) … ( x ℓ , y ℓ , … ) {\displaystyle (x_{1},y_{1},\ldots )(x_{2},y_{2},\ldots )\ldots (x_{\ell },y_{\ell },\ldots )},

where for any index i > |w|, the wi is #.

The zip of x, y, z, ... is denoted zip(x, y, z, ...) or xyz ⋆ ...

The inverse to zip is sometimes denoted unzip.

A variation of the zip operation is defined by:

( x 1 , y 1 , … ) ( x 2 , y 2 , … ) … ( x ℓ _ , y ℓ _ , … ) {\displaystyle (x_{1},y_{1},\ldots )(x_{2},y_{2},\ldots )\ldots (x_{\underline {\ell }},y_{\underline {\ell }},\ldots )}

where ℓ _ {\displaystyle {\underline {\ell }}} is the minimum length of the input words. It avoids the use of an adjoined element # {\displaystyle \#}, but destroys information about elements of the input sequences beyond ℓ _ {\displaystyle {\underline {\ell }}}.

In programming languages

Zip functions are often available in programming languages, often referred to as zip. In Lisp-dialects one can simply map the desired function over the desired lists, map is variadic in Lisp so it can take an arbitrary number of lists as argument. An example from Clojure:

In Common Lisp:

Languages such as Python provide a zip() function. zip() in conjunction with the * operator unzips a list:

Haskell has a method of zipping sequences but requires a specific function for each arity (zip for two sequences, zip3 for three etc.), similarly the functions unzip and unzip3 are available for unzipping:

Language comparison

List of languages by support of zip:

Zip in various languages
LanguageZipZip 3 listsZip n listsNotes
Chapelzip (iter1 iter2)zip (iter1 iter2 iter3)zip (iter1 ... itern)The shape of each iterator, the rank and the extents in each dimension, must be identical.
Clojure(map list list1 list2) (map vector list1 list2)(map list list1 list2 list3) (map vector list1 list2 list3)(map list list1listn) (map vector list1listn)Stops after the length of the shortest list.
Common Lisp(mapcar #'list list1 list2)(mapcar #'list list1 list2 list3)(mapcar #'list list1 ... listn)Stops after the length of the shortest list.
Dzip(range1, range2) range1.zip(range2)zip(range1, range2,range3) range1.zip(range2, range3)zip(range1, …, rangeN) range1.zip(…, rangeN)The stopping policy defaults to shortest and can be optionally provided as shortest, longest, or requiring the same length. The second form is an example of UFCS.
F#List.zip list1 list2 Seq.zip source1 source2 Array.zip array1 array2List.zip3 list1 list2 list3 Seq.zip3 source1 source2 source3 Array.zip3 array1 array2 array3
Haskellzip list1 list2zip3 list1 list2 list3zipn list1listnzipn for n > 3 is available in the module Data.List. Stops after the shortest list ends.
Pythonzip(list1, list2)zip(list1, list2, list3)zip(list1, …, listn)zip() and map() (3.x) stops after the shortest list ends, whereas map() (2.x) and itertools.zip_longest() (3.x) extends the shorter lists with None items
Rubylist1.zip(list2)list1.zip(list2, list3)list1.zip(list1, .., listn)When the list being executed upon (list1) is shorter than the lists being zipped the resulting list is the length of list1. If list1 is longer nil values are used to fill the missing values
Scalalist1.zip(list2)If one of the two collections is longer than the other, its remaining elements are ignored.
Unzip in various languages
LanguageUnzipUnzip 3 tuplesUnzip n tuplesNotes
Clojure(apply map vector ziplist)(apply map vector ziplist)(apply map vector ziplist)
Common Lisp(apply #'mapcar #'list ziplist)(apply #'mapcar #'list ziplist)(apply #'mapcar #'list ziplist)
F#List.unzip list1 list2 Seq.unzip source1 source2 Array.unzip array1 array2List.unzip3 list1 list2 list3 Seq.unzip3 source1 source2 source3 Array.unzip3 array1 array2 array3
Haskellunzip ziplistunzip3 ziplistunzipn ziplistunzipn for n > 3 is available in the module Data.List.
Pythonzip(*zipvlist)zip(*zipvlist)zip(*zipvlist)

See also