0

I have noticed this behavior:

import itertools as iter

truc = iter.count(0)
trac = ["a",'b','c','d','e']
for i in range(3):
    zap = [x for x,y in zip(truc,trac)]
    print(zap)

which outputs

[0, 1, 2, 3, 4]
[6, 7, 8, 9, 10]
[12, 13, 14, 15, 16]

I don't understand why the second list is not ```[5,6,7,8,9]```. And the same goes for the third list, why it does not start at 11 ?

2
  • 1
    zip() advances the iterators in left to right order - it calls next(truc), and then (possibly) next(trac). But when truc yields 5, trac has reached its end, so the overall zip() process ends. So the 5 gets wasted, it has no place in zip's output, and there's no way to stuff it back into the iterator it came from. Commented May 16, 2024 at 1:45
  • @jasonharper : I see, thank you ! Can you make your comment as a response so that I can mark it as an answer ? Commented May 16, 2024 at 2:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.