Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Last iteration?

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
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

 
Reply With Quote
 
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



 
Reply With Quote
 
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


 
Reply With Quote
 
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

 
Reply With Quote
 
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
 
Reply With Quote
 
=?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

 
Reply With Quote
 
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace the last (and only last) character in a string? Johny Python 8 05-03-2007 09:24 PM
how to delete last node of a Linked list if you only know the address of last node. sangram C Programming 16 12-01-2006 08:57 PM
Passed 70-272 last Friday ... now a MCDST at long last! Devo-McDuff MCDST 7 04-20-2006 09:23 PM
last post..some progress =?Utf-8?B?QnVmaGFs?= Wireless Networking 1 11-24-2004 07:25 PM



Advertisments