Yes, this is expected behavior for unpacking an iterator:
>>> lst = [1, 2, 3]
>>> iter(lst)
<list_iterator at 0x7fffe8b84ef0>
The iterator can only be iterated once and then it is exhausted.
>>> i = iter(lst)
>>> next(i)
1
>>> next(i)
2
>>> next(i)
3
>>> next(i)
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-11-a883b34d6d8a> in <module>()
----> 1 next(i)
StopIteration:
The iterator protocol specifies that an exhausted iterator must continue raising StopIteration
exceptions on subsequent calls of its __next__
method. Therefore, iterating it once again is valid (not an error), but the iterator should yield no new items:
>>> list(i)
[]
Nothing prevents you to define an iterator which disobeys this rule, but such iterators are deemed "broken".
The list iterable could be unpacked multiple times, however.
>>> lst = [1, 2, 3]
>>> print(*lst)
1 2 3
>>> print(*lst)
1 2 3
And you could create as many independent iterators, from the same source list, as you want:
>>> i1 = iter(lst)
>>> i2 = iter(lst)
>>> next(i2)
1
>>> next(i2)
2
>>> next(i1) # note: it's another 1
1