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

Schachner, Joseph Joseph.Schachner at Teledyne.com
Fri Sep 7 11:21:50 EDT 2018


The question "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?"
Reflects that you are thinking in a C++ kind of way I think.  

When you declare a variable name and assign to it, in Python what you have done is assigned to the name a reference to the content.   It is never true that the content is "in" the variable.  The variable does not have storage for what you assign to it.  That content is stored somewhere, but the variable just has a reference to it.

So, aList = enumerate(numList) creates an iterator, and aList is a reference to that iterator.  It is not "in" aLIst.   Despite the name, aList does not contain a list.  (See above).

Now, on to the second part: the problem you showed - that you can only loop through aList:print(i,j) once - is BECAUSE you hung onto it from one loop to another.  Once the iterator is exhausted, it's exhausted.  

Think of another more common iterator you've probably used:  file = open("filename.txt")         for line in file;                print line
On EOF the iterator throws an exception (that is expected), the for loop catches the exception and exits.  
Files can be "rewound" by file.seek(0).  But other than that, once you have reached EOF, you have reached EOF.  It doesn't automagically rewind.  If you do another     for line in file;  print line      it will not print anything.  Exactly the same behavior as you are surprised about for the enumerate iterator.

I don’t even know if there is a method to reset the enumerate iterator.  If there isn't one, then you should not hold the iterator from one loop to the next, you have to make another one.

--- Joe S.

-----Original Message-----
From: Viet Nguyen <vhnguyenn at yahoo.com> 
Sent: Thursday, September 6, 2018 2:50 PM
To: python-list at python.org
Subject: Re: 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.



More information about the Python-list mailing list