I\'m trying to learn Python, and I started to play with some code:
a = [3,4,5,6,7]
for b in a:
print(a)
a.pop(0)
<
AFAIK, the for loop uses the iterator protocol. You can manually create and use the iterator as follows:
In [16]: a = [3,4,5,6,7]
...: it = iter(a)
...: while(True):
...: b = next(it)
...: print(b)
...: print(a)
...: a.pop(0)
...:
3
[3, 4, 5, 6, 7]
5
[4, 5, 6, 7]
7
[5, 6, 7]
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
in ()
2 it = iter(a)
3 while(True):
----> 4 b = next(it)
5 print(b)
6 print(a)
The for loop stops if the iterator is exhausted (raises StopIteration
).