Sorting a list

Steve Holden steve at
Tue Oct 28 14:59:28 CET 2008

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

The list's .sort method returns None because it modifies the list
in-place, so after the call it is sorted. So you can either not assign
the list and use unsortedList (whose name now belies its sorted
condition) or you can use

sprtedList = sorted(unsortedList)

since the sorted() function returns a sorted copy of its argument.

Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC    

More information about the Python-list mailing list