I can't tell if this is a confusion about iterators or a confusion about unpacking question.
As bowlofred said, zip returns an iterator. You give it an iterable. It returns an iterator. Each time you call next(iterator) you get the next item in the iterable.
a = ['1-6-2017', '1-13-2017', '1-20-2017', '1-27-2017']
x = zip(a)
print(next(x), next(x))
Output:
('1-6-2017',) ('1-13-2017',)
Notice that zip returns tuples. This is because zip expects you to provide multiple iterables.
a = ['1-6-2017', '1-13-2017', '1-20-2017', '1-27-2017']
b = [265, -10, 130, 330]
c = ['d', '', 'd', '']
x = zip(a, b, c)
print(next(x), next(x))
Output:
('1-6-2017', 265, 'd') ('1-13-2017', -10, '')
If you pass zip 1 iterable, each tuple contains 1 value. If you pass zip 3 iterables, each tuple contains 3 values.
Since zip is an iterator, it will return all the values one at a time and then raise a StopIteration exception.
a = ['1-6-2017', '1-13-2017', '1-20-2017', '1-27-2017']
b = [265, -10, 130, 330]
c = ['d', '', 'd', '']
x = zip(a, b, c)
print(next(x), next(x), next(x), next(x), next(x))
Error:
Traceback (most recent call last):
File "...", line 5, in <module>
print(next(x), next(x), next(x), next(x), next(x))
StopIteration
Normally you don't have to worry about the StopIteration exception because you use zip() in a for loop, and "for" processes the exception as the end of loop condition.
a, b, and c each have 4 items, so zip(a, b, c) can be iterated 4 times before raising the StopIteration exception.
Python supports packing and unpacking sequences. An iterator is a sequence, so you can unpack an iterator.
a = ['1-6-2017', '1-13-2017', '1-20-2017', '1-27-2017']
b = [265, -10, 130, 330]
c = ['d', '', 'd', '']
x, y, *z = zip(a, b, c)
print(x, y, z)
Output:
('1-6-2017', 265, 'd') ('1-13-2017', -10, '') [('1-20-2017', 130, 'd'), ('1-27-2017', 330, '')]
I told Python that z will take all remaining values from the iterator, and when printing you can see that z contains two values, not one. This makes sense. a, b and c each are each len 4. zip(a, b, c) will produce 4 values before raising the StopIteration exception. Unpacking puts the first item in x, the second in y, and the two remaining items in z. There are 4 items to unpack. 4 items cannot be unpacked into 3 variables (well, they can, but you know what I mean).
When I first read you post I thought you were trying to transpose this group of lists:
Quote:a = ['1-6-2017', '1-13-2017', '1-20-2017', '1-27-2017']
b = [265, -10, 130, 330]
c = ['d', '', 'd', '']
Into somethin like this:
Quote:[('1-6-2017', 265, 'd'), ('1-13-2017', -10, ''), ('1-20-2017', 130, 'd'), ('1-27-2017', 330, '')]
The Python for doing that is:
x = list(zip(a, b, c))
And I could then iterate over the tuples in x.
for xp, yp, m in x:
print(xp, yp, m)
Output:
1-6-2017 265 d
1-13-2017 -10
1-20-2017 130 d
1-27-2017 330
Now I am unpacking tuples, not an iterator. Each tuple is len 3, so I need 3 variables to unpack.