Velocity Reviews > Last iteration?

# Last iteration?

Florian Lindner
Guest
Posts: n/a

 10-12-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

Can this be acomplished somehow?

Thanks,

Florian

Stefan Behnel
Guest
Posts: n/a

 10-12-2007
Florian Lindner wrote:
> 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?

You could do this:

l = [1,2,3]
s = len(l) - 1
for i, item in enumerate(l): # Py 2.4
if i == s:
print item*item
else:
print item

Or, you could look one step ahead:

l = [1,2,3]
next = l[0]
for item in l[1:]:
print next
next = item
print next * next

Stefan

Diez B. Roggisch
Guest
Posts: n/a

 10-12-2007
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
>
>
> Can this be acomplished somehow?

def last_iter(iterable):
it = iter(iterable)
buffer = [it.next()]
for i in it:
buffer.append(i)
old, buffer = buffer[0], buffer[1:]
yield False, old
yield True, buffer[0]

for last, i in last_iter(xrange(4)):
if last:
print i*i
else:
print i

Diez

Paul Hankin
Guest
Posts: n/a

 10-12-2007
On Oct 12, 11:58 am, Florian Lindner <(E-Mail Removed)> 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

Yes, either use enumerate or just stop the loop early and deal with
the last element outside the loop.

xs = [1, 2, 3]
for x in xs[:-1]:
print x
print xs[-1] * xs[-1]

--
Paul Hankin

tasjaevan@gmail.com
Guest
Posts: n/a

 10-12-2007
On Oct 12, 11:58 am, Florian Lindner <(E-Mail Removed)> 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

Carsten Haese
Guest
Posts: n/a

 10-12-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

Paul Hankin
Guest
Posts: n/a

 10-12-2007
On Oct 12, 2:18 pm, Carsten Haese <(E-Mail Removed)> wrote:
> 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

Nice! A more 'readable' version is:

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

--
Paul Hankin

Peter Otten
Guest
Posts: n/a

 10-12-2007
Diez B. Roggisch wrote:

> Florian Lindner wrote:

>> can I determine somehow if the iteration on a list of values is the
>> last iteration?

> def last_iter(iterable):
> it = iter(iterable)
> buffer = [it.next()]
> for i in it:
> buffer.append(i)
> old, buffer = buffer[0], buffer[1:]
> yield False, old
> yield True, buffer[0]

This can be simplified a bit since you never have to remember more than on
item:

>>> def mark_last(items):

.... items = iter(items)
.... last = items.next()
.... for item in items:
.... yield False, last
.... last = item
.... yield True, last
....
>>> list(mark_last([]))

[]
>>> list(mark_last([1]))

[(True, 1)]
>>> list(mark_last([1,2]))

[(False, 1), (True, 2)]

Peter

=?iso-8859-1?q?Robin_K=E5veland?=
Guest
Posts: n/a

 10-13-2007
On Oct 12, 12:58 pm, Florian Lindner <(E-Mail Removed)> 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?
>
> Thanks,
>
> Florian

If you want to do this over a list or a string, I'd just do:

for element in iterable[:-1]: print iterable
print iterable[-1] * iterable[-1]

No need for it to get more advanced than that

Paul McGuire
Guest
Posts: n/a

 10-14-2007
On Oct 12, 5:58 am, Florian Lindner <(E-Mail Removed)> 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?
>
> Thanks,
>
> Florian

Maybe it's a leftover from my C++ days, but I find the iteration-based
solutions the most appealing. This is a refinement of the previous
post by Diez Roggisch. The test method seems to match your desired
idiom pretty closely:

def signal_last(lst):
last2 = None
it = iter(lst)
try:
last = it.next()
except StopIteration:
last = None
for last2 in it:
yield False, last
last = last2
yield True, last

def test(t):
for isLast, item in signal_last(t):
if isLast:
print "...and the last item is", item
else:
print item

test("ABC")
test([])
test([1,2,3])

Prints:

A
B
....and the last item is C
....and the last item is None
1
2
....and the last item is 3

-- Paul