Sorting a list

duncan smith buzzard at freeserve.co.uk
Tue Oct 28 21:06:36 EDT 2008


RC wrote:
> unsortedList = list(["XYZ","ABC"])
> 
> sortedList = unsortedList.sort()
> print sortedList
> 
> 
> Why this return None?

Because the sort method sorts the list in place (and returns None).

> How do I get return as ["ABC", "XYZ"]?

>>> unsortedList = ["XYZ","ABC"]
>>> unsortedList.sort()
>>> unsortedList
['ABC', 'XYZ']

or if you want a distinct sorted list (leaving the original list unchanged)

>>> unsortedList = ["XYZ","ABC"]
>>> sortedList = sorted(unsortedList)
>>> unsortedList
['XYZ', 'ABC']
>>> sortedList
['ABC', 'XYZ']
>>>

Duncan



More information about the Python-list mailing list