Confused about a list.sort()
Terry Carroll
carroll at tjc.com
Tue Jan 27 23:05:46 EST 2004
On Tue, 27 Jan 2004 18:28:09 -0800, "Amy G" <amy-g-art at cox.net> wrote:
>I have a list of numbers... actully I have two lists, List 1 is a list of
>number strings and List2 is one of numbers.
>
>List 1 example:
>List1 = [ '20040124123000', '20040124123001', '20040125012456']
>
>List 2 example:
>List2 = [ 20040124123000L, 20040124123001L, '20040125012456L]
>
>When I try either:
>List1 = List1.sort ... or
>List2 = List2.sirt
>
>and then...
>print List1... or
>print List2
>
>I get None.
>
>Why is this?
Yeah, most everyone who uses sort() for the first time gets bit by this.
Sort() sorts the list in place, and returns None:
>>> list1=[20, 40, 60, 80, 10, 30, 50]
>>> list1
[20, 40, 60, 80, 10, 30, 50]
>>> list1.sort()
>>> list1
[10, 20, 30, 40, 50, 60, 80]
So, to sort list1, you just use list1.sort(), not foo = list1.sort()
More information about the Python-list
mailing list