zip
zip(*iterables)
The real name of
zip is transpose.If you try to choose certain indices of each element in
iterables and group them together, you need zip. It is transpose operation.Thinking arguments of zip as a matrix, each argument is a row. Then [0, 1, 2, 3, 4], [5, 6, 7, 8, 9] looks like
[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]
zip will transpose it to:
[0, 5],
[1, 6],
[2, 7],
[3, 8],
[4, 9]
Example
>>> list(zip([0, 1, 2, 3, 4], [5, 6, 7, 8, 9]))
[(0, 5), (1, 6), (2, 7), (3, 8), (4, 9)]
Example: inversive
Transpose is inversive which means \(({A^T})^T = A\). Here is an example showing this feather of zip:
>>> res = list(zip(*(zip([0, 1, 2, 3, 4], [5, 6, 7, 8, 9]))))
>>> list(res)
[(0, 1, 2, 3, 4), (5, 6, 7, 8, 9)]
Note
ziptakes*iterablesas arguments which are argument seperated by comma.The star in
zip(*zip(...unpack zip object to iterable.list()is needed to explicit get zip result.