Python zip() implements structuring
The
zip()
function combines data from multiple iterables or sequences, converting an iterable or sequence with n elements into n-tuples. The previous example uses the zip()
function to combine data from two sample sets, generating a sequence of two-tuples.
The
zip()
function returns a generator and does not instantiate the result.
zip()
does the following:
>>> xi = [1.47, 1.50, 1.52, 1.55, 1.57, 1.60, 1.63, 1.65,
... 1.68, 1.70, 1.73, 1.75, 1.78, 1.80, 1.83,]
>>> yi = [52.21, 53.12, 54.48, 55.84, 57.20, 58.57, 59.93, 61.29,
... 63.11, 64.47, 66.28, 68.10, 69.92, 72.19, 74.46,]
>>> zip(xi, yi)
<zip object at 0x101d62ab8>
>>> list(zip(xi, yi))
[(1.47, 52.21), (1.5, 53.12), (1.52, 54.48), (1.55, 55.84),
(1.57, 57.2), (1.6, 58.57), (1.63, 59.93), (1.65, 61.29),
(1.68, 63.11), (1.7, 64.47), (1.73, 66.28), (1.75, 68.1),
(1.78, 69.92), (1.8, 72.19), (1.83, 74.46)]
zip()
needs to handle some special cases. For example, we need to know how it operates in the following situations.
- What happens if there are no input arguments?
-
What happens if there is only one input argument?
-
What happens if the sequences given as input arguments have different lengths?
For other functions (such as any()
, all()
, len()
, and sum()
), we need to know what the identity is when reducing an empty sequence. For example, sum()
returns 0, and this concept can be used to determine the identity of the zip()
function.
Obviously, each of these special cases generates some kind of iterable object. The following code demonstrates the behavior of the zip()
function in these cases. The case with an empty input argument is as follows:
>>> zip()
<zip object at 0x101d62ab8>
>>> list(_)
[]
As you can see, the zip()
function with no input arguments returns a generator function, but it contains no items, which satisfies the requirement for an iterable output object.
The case with a single input parameter is as follows:
>>> zip((1,2,3))
<zip object at 0x101d62ab8>
>>> list(_)
[(1,), (2,), (3,)]
In this case, the zip()
function returns a sequence of 1-tuples, which also meets the requirements.
The behavior of the zip()
function when the input sequence lengths are different is as follows:
>>> list(zip((1, 2, 3), ('a', 'b')))
[(1, 'a'), (2, 'b')]
This result has caused controversy within the Python community. Why truncation? Why not fill shorter lists with None
values? As an alternative to the zip()
function, the zip_longest()
function from the itertools
module satisfies the above requirements.