Last iteration?

tasjaevan at gmail.com tasjaevan at gmail.com
Fri Oct 12 08:13:04 EDT 2007


On Oct 12, 11:58 am, Florian Lindner <Florian.Lind... at xgm.de> wrote:
> 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
>
> Can this be acomplished somehow?
>

Another suggestion:

  l = [1, 2, 3]
  for i in l[:-1]: print i
  i = l[-1]
  print i*i


James






More information about the Python-list mailing list