Zipping (computer science)

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 $$\ell$$ denote the length of the longest word which is fish; $$\ell = 4$$. The zip of cat, fish, be is then 4 tuples of elements:


 * $$ (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 $$\underline{\ell}$$, where $$\underline{\ell} = 2$$:

Definition
Let &Sigma; be an alphabet, # a symbol not in &Sigma;.

Let x1x2... xundefined, y1y2... yundefined, z1z2... zundefined, ... be n words (i.e. finite sequences) of elements of &Sigma;. Let $$\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 $(&Sigma; ∪ {#})$, i.e. an element of $$((\Sigma\cup\{\# \})^n)^*$$:


 * $$ (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 x ⋆ y ⋆ z ⋆ ...

The inverse to zip is sometimes denoted unzip.

A variation of the zip operation is defined by:


 * $$ (x_1,y_1,\ldots)(x_2,y_2,\ldots)\ldots(x_{\underline{\ell}},y_{\underline{\ell}},\ldots)$$

where $$\underline{\ell}$$ is the minimum length of the input words. It avoids the use of an adjoined element $$\#$$, but destroys information about elements of the input sequences beyond $$\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: