Sorting a list
Steve Holden
steve at holdenweb.com
Tue Oct 28 09:59:28 EDT 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.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
More information about the Python-list
mailing list