On 14/09/10 02:24, Pete O'Connell wrote:
Hi I don't actaully need the list printed reversed, I need it printed
from smallest number to largest

Just sorting gives you the list from smallest to largest.
For example:
#############

theList = ["21 trewuuioi","374zxc","13447"]
print sorted(theList)
['13447', '21 trewuuioi', '374zxc']
#the rabove result is not what I want
################

If I reverse that I still don't get the items in numerical order. The
result I want is:
['21 trewuuioi','374zxc','13447']
Before you said you wanted it like this:  ['3zxc','21 trewuuioi','134445']

The order above isn't in any obvious numerical order.
Anyway I think I see where you are coming from. In my previous example I forgot to assign the sorted list back to theList. Hopefully the following will make this clear.

>>> theList = ["21 trewuuioi","3zxc","134445"]
>>> sorted(theList)
['134445', '21 trewuuioi', '3zxc']
>>> theList
['21 trewuuioi', '3zxc', '134445']
>>> theList = sorted(theList)
>>> theList
['134445', '21 trewuuioi', '3zxc']
>>> theList[::-1]
['3zxc', '21 trewuuioi', '134445']
>>> theList.reverse()
>>> theList
['3zxc', '21 trewuuioi', '134445']

as you can see sorted makes a new list rather than modifying your original.
On Tue, Sep 14, 2010 at 10:41 AM, Adam Bark<adam.jt...@gmail.com>  wrote:
On 14/09/10 01:11, Pete O'Connell wrote:
theList = ["21 trewuuioi","3zxc","134445"]
print sorted(theList)

Hi, the result of the sorted list above doesn't print in the order I
want. Is there a straight forward way of getting python to print
['3zxc','21 trewuuioi','134445']
rather than ['134445', '21 trewuuioi', '3zxc']?

Any help would be greatly appreciated
Pete

print sorted(theList)[::-1]

as list indices go [start:end:step] what this means is the whole list
starting from the end and working backwards.
You can also have a look at reversed() if you want an iterator or you can
use theList.reverse() if you want to reverse in place ie.

l = ["21 trewuuioi","3zxc","134445"]
l.reverse()
print l
['134445', '3zxc', '21 trewuuioi']


HTH
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor




_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to