Newbie question - sorting a slice

half.italian at gmail.com half.italian at gmail.com
Tue Aug 28 23:01:10 EDT 2007


On Aug 28, 7:43 pm, hwg <hwg... at yahoo.com> wrote:
> I've searched the group and didn't see the answer to this...
>
> Why doesn't this work?:
>
> >>> letters = ['d', 'a', 'e', 'c', 'b']
> >>> letters[1:3].sort()
>
> This returns None.
>
> Why?  letters[1:3]  is  ['a', 'e', 'c']    Sorting that should return
> ['a', 'c', 'e']
>
> hwg

sort() works on a list in place, and returns none

You have to make a temp list.

note...
>>> letters = ['d', 'a', 'e', 'c', 'b']
>>> print letters.sort()
None
>>> print letters
['a', 'b', 'c', 'd', 'e']

~Sean




More information about the Python-list mailing list