Last iteration?

Andreas Tawn andreas.tawn at ubisoft.com
Fri Oct 12 07:23:19 EDT 2007


> Hello,
> can I determine somehow if the iteration on a list of values 
> is the last
> iteration?
> 
> Example:
> 
> for i in [1, 2, 3]:
>    if last_iteration:
>       print i*i
>    else:
>       print i
> 
> that would print
> 
> 1
> 2
> 9

Something like:

myList = [1, 2, 3]
for i, j in enumerate(myList):
    if i == len(myList)-1:
        print j*j
    else:
        print j

Cheers,

Andreas Tawn
Lead Technical Artist
Ubisoft Reflections



More information about the Python-list mailing list