Access last element after iteration

Jon Ribbens jon+usenet at unequivocal.eu
Tue Jul 7 08:37:34 EDT 2020


On 2020-07-07, Frank Millman <frank at chagford.com> wrote:
> After iterating over a sequence, the final element is still accessible. 
> In this case, the variable 'i' still references the integer 4.
...
> Is this guaranteed in Python, or should it not be relied on?

It is guaranteed, *except* if the sequence is empty and therefore the
loop never executes at all, the variable will not have been assigned
to and therefore may not exist.


More information about the Python-list mailing list