(Oct-09-2023, 01:12 PM)akbarza Wrote: when you write and run the first for loop(lines 6.7), some data will be shown to you. but then if you write and run the for loop again (lines 9,10), nothing will be displayed to you, also as you can see in the code, list(e) is empty. why?
thanks
Not "some data" but "all data".
enumerate() creates enumerate object which in turn uses iterator's __next()__ method. So iterator gets exhausted:
Quote:A container object (such as a list) produces a fresh new iterator each time you pass it to the iter() function or use it in a for loop. Attempting this with an iterator will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container.
So:
>>> e = enumerate(range(10, 13)) >>> print(*e) (0, 10) (1, 11) (2, 12) >>> print(*e) >>> e = enumerate(range(10, 13)) >>> e = list(enumerate(range(10, 13))) # make list from enumerate object for reuse >>> print(*e) (0, 10) (1, 11) (2, 12) >>> print(*e) (0, 10) (1, 11) (2, 12)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.