Why emumerated list is empty on 2nd round of print?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Thu Sep 6 21:56:13 EDT 2018
On Thu, 06 Sep 2018 11:50:17 -0700, Viet Nguyen via Python-list wrote:
> If I do this "aList = enumerate(numList)", isn't it
> stored permanently in aList now?
Yes, but the question is "what is *it* that is stored? The answer is, it
isn't a list, despite the name you choose. It is an enumerate iterator
object, and iterator objects can only be iterated over once.
If you really, truly need a list, call the list constructor:
aList = list(enumerate(numList))
but that's generally a strange thing to do. It is more common to just
call enumerate when you need it, not to hold on to the reference for
later.
--
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson
More information about the Python-list
mailing list