Make a iterator that aggregates elements from each of the iterables.
Returns an iterator of tuples, where the i-th tuple contains the i--th element from each of the argument Sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator. Equivalent to:
def zip (*iterables): # Zip (' ABCD ', ' xy ')--Ax by Sentinel = Object () iterators = [iter (it) to it in ITER Ables] While iterators: result = [] for it in iterators: elem = Next (it, Sentinel) if Elem is Sentin El: return result.append (elem) yield tuple (result)
The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using zip(*[iter(s)]*n)
. This repeats, the same iterator times so, each output tuple have the result of calls to the n
n
Iterato R. This have the effect of dividing the input into n-length chunks.
zip()
Should only is used with unequal length inputs if you don ' t care about trailing, unmatched values from the longer iterab Les. If Those values is important, use itertools.zip_longest()
instead.
zip()
In conjunction with the *
operator can is used to unzip a list:
>>>
>>> x = [1, 2, 3]>>> y = [4, 5, 6]>>> zipped = Zip (x, y) >>> list (zipped) [(1, 4), (2, 5 ), (3, 6)]>>> x2, y2 = Zip (*zip (x, y)) >>> x = = List (x2) and y = = List (y2) True