git.net

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

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


On Thursday, September 6, 2018 at 10:34:19 AM UTC-7, Chris Angelico wrote:
> On Fri, Sep 7, 2018 at 3:26 AM, Viet Nguyen via Python-list
> <python-list at python.org> wrote:
> >>>> numList
> > [2, 7, 22, 30, 1, 8]
> >
> >>>> aList = enumerate(numList)
> >
> >>>> for i,j in aList:print(i,j)
> >
> > 0 2
> > 1 7
> > 2 22
> > 3 30
> > 4 1
> > 5 8
> >
> >>>> for i,j in aList:print(i,j)
> >
> >>>>
> 
> Because it's not an enumerated list, it's an enumerated iterator.
> Generally, you'll just use that directly in the loop:
> 
> for i, value in enumerate(numbers):
> 
> There's generally no need to hang onto it from one loop to another.
> 
> ChrisA

Thanks ChrisA. If I do this "aList = enumerate(numList)", isn't it stored permanently in aList now?  I see your point to use it directly, but just in case I do need to hang onto it from one loop to another, then how is that done?   Anyway I think I'm ok and I got what I need for now.