for x in y:
for x in X:
the Python interpreter checks whether X has an iterator interface.
The iterator interface can be checked with the built-in iter
function:
iter(X)
for
loopit gives access to the next object for as long as it's valid, with
the built-in function next
:
I = iter([2, 4, 6, 8, 10])
print(next(I)) -> 2
print(next(I)) -> 4
print(next(I)) -> 6
>>> L = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
>>> for le,ri in zip(range(1, 11), L) : print(le, ri)
https://nbviewer.jupyter.org/github/jakevdp/WhirlwindTourOfPython/blob/master/10-Iterators.ipynb