[Tutor] (no subject)

Don Arnold darnold02@sprynet.com
Wed, 2 Oct 2002 06:15:07 -0500


----- Original Message -----
From: "Annika Scheffler" <annika.scheffler@gmx.net>
To: <tutor@python.org>
Sent: Wednesday, October 02, 2002 3:48 AM
Subject: [Tutor] (no subject)


> Hi Rob,
>
> Thanks for your help! In fact, it does work that way for me, too. But when
> writing a function like this:
>
> def sortList(someList):
>
>
>           return someList.sort()
>
> print sortList("What have we here".split())
>
> I get "none" as a result. Why's that?
>
> Thanks,
> Annika
>
>
<snip>

Hello. You get None because the sort() method modifies the list you give it.
It doesn't create a new list, so there really is nothing for it to return.
You'll want to return the list itself after you sort it. Also, since lists
are mutable, you may want to work with a copy of the list to leave your
original list intact:

>>> def sortList(someList):
    tempList = someList[:]
    tempList.sort()
    return tempList

>>> b = [4,3,2,1]
>>> a = sortList(b)
>>> print a
[1, 2, 3, 4]
>>> print b
[4, 3, 2, 1]
>>>

Hope that helps,
Don