A few beginning questions

Duncan Booth duncan at NOSPAMrcp.co.uk
Wed Jul 16 11:04:18 CEST 2003

mwilson at the-wire.com (Mel Wilson) wrote in news:AhCF/ks/KjWP089yn at the-

> In article <Xns93B968108D2DCduncanrcpcouk at>,
> Duncan Booth <duncan at NOSPAMrcp.co.uk> wrote:
>>The usual way is simply to reverse the list before iterating over it.
>>     l = ['some', 'text', 'in', 'a', 'list' ]
>>     l_reversed = l.reverse()
>>     for item in l_reversed:
>>         do something to item
> Trouble.  l.reverse() reverses l in place.  So just
>         l.reverse()
>         for item in l:


>>> for i in range(100):
	print "I must test code before posting it to Usenet"

Thanks for correcting that stupid blunder.

Yes, if it is safe to reverse the original list, just reverse and iterate 
over it as corrected above. If not, copy and reverse it:

>>> l = ['some', 'text', 'in', 'a', 'list' ]
>>> l_reversed = list(l)
>>> l_reversed.reverse()
>>> for item in l_reversed:
	print item


(Both code snippets tested this time.)

Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?

More information about the Python-list mailing list