OSDir


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Why emumerated list is empty on 2nd round of print?


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