473,745 Members | 2,100 Online

# Last iteration?

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
Oct 12 '07 #1
23 18074
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
Oct 12 '07 #2
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(itera ble):
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(xrang e(4)):
if last:
print i*i
else:
print i

Diez
Oct 12 '07 #3
On Oct 12, 11:58 am, Florian Lindner <Florian.Lind.. .@xgm.dewrote:
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

Oct 12 '07 #4
On Oct 12, 11:58 am, Florian Lindner <Florian.Lind.. .@xgm.dewrote:
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

Oct 12 '07 #5
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(e numerate(revers ed(mylist)))):
if j==0:
print i*i
else:
print i

;)

--
Carsten Haese
http://informixdb.sourceforge.net
Oct 12 '07 #6
On Oct 12, 2:18 pm, Carsten Haese <cars...@uniqsy s.comwrote:
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(e numerate(revers ed(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(e numerate(revers ed(mylist)))):
if not_last:
print i
else:
print i * i

--
Paul Hankin

Oct 12 '07 #7
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(itera ble):
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_las t([]))
[]
>>list(mark_las t([1]))
[(True, 1)]
>>list(mark_las t([1,2]))
[(False, 1), (True, 2)]

Peter
Oct 12 '07 #8
On Oct 12, 12:58 pm, Florian Lindner <Florian.Lind.. .@xgm.dewrote:
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 :)

Oct 13 '07 #9
On Oct 12, 5:58 am, Florian Lindner <Florian.Lind.. .@xgm.dewrote:
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

Oct 14 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.