Re: Iterating a list in reverse ?

2006-06-21 Thread Andy Dingley <[EMAIL PROTECTED]>
Fredrik Lundh wrote:

> for item in reversed(listOfThings):

Thanks! I was staring so hard at reverse() that I'd completely missed
reversed()

I think I prefer this to listOfThings[::-1]:  as it's a little more
readable.
Not that I'm reacting to past bad experience of Perl, you understand
8-)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Iterating a list in reverse ?

2006-06-21 Thread Luis M. González

Andy Dingley <[EMAIL PROTECTED]> wrote:
> Python newbie:  I've got this simple task working (in about ten
> different ways), but I'm looking for the "favoured" and "most Python
> like" way.
>
> Forwards I can do this
> for t in listOfThings:
> print t
>
> Now how do I do it in reverse?   In particular, how might I do it if I
> only wanted to iterate part-way through (with a conditional test and a
> break), or if I had a large list ?
>
> reverse( listOfThings )
> for t in listOfThings:
> print t


listOfThings = [1,2,3,4,5,6]

for i in listOfThings:
print i# print from 1 to 6


 for i in listOfThings[::-1]:
print i # prints from 6 to 1

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [newbie] Iterating a list in reverse ?

2006-06-21 Thread Fredrik Lundh
Andy Dingley <[EMAIL PROTECTED]> wrote:

> Python newbie:  I've got this simple task working (in about ten
> different ways), but I'm looking for the "favoured" and "most Python
> like" way.
> 
> Forwards I can do this
> for t in listOfThings:
> print t
> 
> Now how do I do it in reverse?   In particular, how might I do it if I
> only wanted to iterate part-way through (with a conditional test and a
> break), or if I had a large list ?
> 
> reverse( listOfThings )
> for t in listOfThings:
> print t

for item in reversed(listOfThings):
 ...

 >>> help(reversed)
Help on class reversed in module __builtin__:

class reversed(object)
  |  reversed(sequence) -> reverse iterator over values of the sequence
  |
  |  Return a reverse iterator

...

> As reverse() operates in-place I often can't do this.

given that lists only hold references to objects, reversing a *copy* of 
the list is a lot more efficient than you may think...



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [newbie] Iterating a list in reverse ?

2006-06-21 Thread Tim Chase
> Python newbie:  I've got this simple task working (in about ten
> different ways), but I'm looking for the "favoured" and "most Python
> like" way.
> 
> Forwards I can do this
> for t in listOfThings:
> print t
> 
> Now how do I do it in reverse?   

Then general process would be to use the reversed() iterator:

for t in reversed(listOfThings):
print t

Python provides a sorted() wrapper of the same non-in-place form.

-tkc



-- 
http://mail.python.org/mailman/listinfo/python-list


[newbie] Iterating a list in reverse ?

2006-06-21 Thread Andy Dingley <[EMAIL PROTECTED]>
Python newbie:  I've got this simple task working (in about ten
different ways), but I'm looking for the "favoured" and "most Python
like" way.

Forwards I can do this
for t in listOfThings:
print t

Now how do I do it in reverse?   In particular, how might I do it if I
only wanted to iterate part-way through (with a conditional test and a
break), or if I had a large list ?

reverse( listOfThings )
for t in listOfThings:
print t

As reverse() operates in-place I often can't do this. I'm also
(slightly) concerned about possible inefficiency issues of manipulating
a big list just to scan a peek at its tail.

Currently I'm doing this:

for i in range( len( listOfThings )-1, 0, -1):
t = listOfThings  [i]
print t

Is this the optimum ?   Would xrange() be a better choice (and when is
it a "big" list) ?



Thanks for any guidance

-- 
http://mail.python.org/mailman/listinfo/python-list