It seems that converting iter. to list move the iter index to last element, why ?
l = iter([1, 2, 3])
x = list(l)
print l.next()
Error message:
Traceback (most recent call last): print l.next()
StopIteration
It seems that converting iter. to list move the iter index to last element, why ?
l = iter([1, 2, 3])
x = list(l)
print l.next()
Error message:
Traceback (most recent call last): print l.next()
StopIteration
Because firstly, you are creating a new object. Secondly an iterator object doesn't have all it's item in memory it generates the item on demand, and once you got to the end there is no way back. That's why they call it one shot iterable. Therefor when you call the list
on an iterator you are creating new list object by consuming the iterator and preserving its items in a list.
For a better demonstration it does something like following:
In [8]: l = iter([1, 2, 3])
In [9]: x = [item for item in l]
In [10]: x
Out[10]: [1, 2, 3]