[newbie] Iterating a list in reverse ?

A

Andy Dingley

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
print t

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



Thanks for any guidance
 
F

Fredrik Lundh

Andy Dingley said:
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 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...

</F>
 
T

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
 
?

=?iso-8859-1?q?Luis_M._Gonz=E1lez?=

Andy Dingley said:
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
 
A

Andy Dingley

Fredrik said:
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
:cool:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top