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 denote the length of the longest word which is fish;. The zip of cat, fish, be is then 4 tuples of elements:
where # is a symbol not in the original alphabet. In Haskell this truncates to the shortest sequence, where :

zip3 "cat" "fish" "be"
--

Definition

Let Σ be an alphabet, # a symbol not in Σ.
Let x1x2... x|x|, y1y2... y|y|, z1z2... z|z|,... be n words of elements of Σ. Let 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 :
where for any index, the wi is #.
The zip of x, y, z,... is denoted zip or xyz ⋆...
The inverse to zip is sometimes denoted unzip.
A variation of the zip operation is defined by:
where 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.

In programming languages

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

;; `nums' contains an infinite list of numbers
;; To zip and into a vector, invoke `map vector' on them; same with list
; ⇒
; ⇒ )
; ⇒
;; `map' truncates to the shortest sequence; note missing \c and \e from "Alice"
; ⇒
; ⇒
;; To unzip, apply `map vector' or `map list'
;; ⇒ )

In Common Lisp:

;; ⇒ )
;; ⇒ ) — truncates on shortest list
;; Unzips
;; ⇒ )

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

>>> nums =
>>> tens =
>>> firstname = 'Alice'
>>> zipped = list
>>> zipped
>>> list # unzip
>>> zipped2 = list
>>> zipped2 # zip, truncates on shortest

>>> list # unzip

Haskell has a method of zipping sequences but requires a specific function for each arity, similarly the functions and are available for unzipping:

-- nums contains an infinite list of numbers
nums =
tens =
firstname = "Alice"
zip nums tens
-- ⇒ — zip, truncates infinite list
unzip $ zip nums tens
-- ⇒ — unzip
zip3 nums tens firstname
-- ⇒ — zip, truncates
unzip3 $ zip3 nums tens firstname
-- ⇒ — unzip

Language comparison

List of languages by support of zip:
LanguageZipZip 3 listsZip n listsNotes
ChapelThe shape of each iterator, the rank and the extents in each dimension, must be identical.
Clojure


Stops after the length of the shortest list.
Common LispStops after the length of the shortest list.
D


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#



Haskell for n > 3 is available in the module Data.List. Stops after the shortest list ends.
Python and stops after the shortest list ends, whereas and extends the shorter lists with items
RubyWhen the list being executed upon 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
ScalaIf one of the two collections is longer than the other, its remaining elements are ignored.

LanguageUnzipUnzip 3 tuplesUnzip n tuplesNotes
Clojure
Common Lisp
F#



Haskell for n > 3 is available in the module
Python