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.
Given the three words cat, fish and be where |cat| is 3, |fish| is 4 and |be| is 2. Let
\ell
\ell=4
(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}
\underline{\ell}=2
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
\ell
The zip of these words is a finite sequence of n-tuples of elements of, i.e. an element of
((\Sigma\cup\{\#\})n)*
(x1,y1,\ldots)(x2,y2,\ldots)\ldots(x\ell,y\ell,\ldots)
where for any index, 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:
(x1,y1,\ldots)(x2,y2,\ldots)\ldots(x\underline{\ell
\underline{\ell}
\#
\underline{\ell}
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:[1]
In Common Lisp:
(mapcar #'list nums tens)
(mapcar #'list nums tens (coerce firstname 'list))
Languages such as Python provide a function.[2] in conjunction with the operator unzips a list:[2]
>>> zipped = list(zip(nums, tens))>>> zipped[(1, 10), (2, 20), (3, 30)]
>>> list(zip(*zipped)) # unzip[(1, 2, 3), (10, 20, 30)]
>>> zipped2 = list(zip(nums, tens, list(firstname)))>>> zipped2 # zip, truncates on shortest[(1, 10, 'A'), (2, 20, 'l'), (3, 30, 'i')] >>> list(zip(*zipped2)) # unzip[(1, 2, 3), (10, 20, 30), ('A', 'l', 'i')]
Haskell has a method of zipping sequences but requires a specific function for each arity (for two sequences, for three etc.),[3]
zip nums tens-- ⇒ [(1,10), (2,20), (3,30)] — zip, truncates infinite listunzip $ zip nums tens-- ⇒ ([1,2,3], [10,20,30]) — unzip
zip3 nums tens firstname-- ⇒ [(1,10,'A'), (2,20,'l'), (3,30,'i')] — zip, truncatesunzip3 $ zip3 nums tens firstname-- ⇒ ([1,2,3], [10,20,30], "Ali") — unzip
List of languages by support of zip:
Language | Zip | Zip 3 lists | Zip n lists | Notes | |
---|---|---|---|---|---|
Chapel | The shape of each iterator, the rank and the extents in each dimension, must be identical.[3] | ||||
Clojure | Stops after the length of the shortest list. | ||||
Common Lisp | Stops 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.[4] 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 (3.x) stops after the shortest list ends, whereas (2.x) and (3.x) extends the shorter lists with items | ||||
Ruby | 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[5] | ||||
Scala | If one of the two collections is longer than the other, its remaining elements are ignored.[6] |
Language | Unzip | Unzip 3 tuples | Unzip n tuples | Notes | |
---|---|---|---|---|---|
Clojure | |||||
Common Lisp | |||||
F# | |||||
Haskell | for n > 3 is available in the module | ||||
Python |