Last iteration?

Carsten Haese carsten at uniqsys.com
Fri Oct 12 15:18:20 CEST 2007


On Fri, 2007-10-12 at 12:58 +0200, Florian Lindner 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

Here's another solution:

mylist = [1,2,3]
for j,i in reversed(list(enumerate(reversed(mylist)))):
  if j==0:
     print i*i
  else:
     print i

;)

-- 
Carsten Haese
http://informixdb.sourceforge.net





More information about the Python-list mailing list