Back to questions
JuniorTheoryCommon
What does the program print?
it = iter([1, 2])
print(next(it), next(it), next(it, "end"))1 2 end
- Expecting a
StopIterationon the third call: it comes only fromnext(it)with one argument; with a second one,nextreturns that value when the iterator is used up. - Expecting
1 1, as ifnextread the list from the start each time: the iterator remembers its position, so every call moves one item further.
- What happens if you call
next(it)once more after the third call? - How does a
forloop useiterandnextunder the hood?