[BangPypers] sort query
Sidharth Kuruvila
sidharth.kuruvila at gmail.com
Sat Oct 24 13:39:25 CEST 2009
Hi,
The sort method doesn't return anything. It modifies the list which
you call it on.
l = d.keys()
l.sort() #There is no point assigning the return value to this because
it will be None
#Which is what you do when you write l = d.keys().sort()
print l
<prints the keys in sorted order>
Now there is another subtle issue here with d.keys(), ever time you
call it, you get a new list object, not the one you got with the
previous call. So don't expect d.keys() to return a sorted list.
Also, it's quite clear you didn't understand any of the previous mails
because Nauful did bring up the lack of a return value for sort in his
mail. If you don't understand something ask.
An important point is if you find something wrong with an api, it is
more likely that there is something wrong with your understanding than
the api itself. This is something most of us have to learn the hard
way. So do a lot of hard thinking before you say something is broken.
Regards,
Sidharth
On Sat, Oct 24, 2009 at 4:49 PM, bhaskar jain
<bhaskar.jain2002 at gmail.com> wrote:
> Thanks all for replying.
>
> Let me be clear,
>
>>>> l = [2,1]
>
>>>> id(l[0])
> 8402300
>>>> id(l[1])
> 8402312
>
>>>> l.sort()
>
>>>> id(l[0])
> 8402312
>>>> id(l[1])
> 8402300
>
> So if we had [l] ------> [0] -----> 2
> [1] -----> 1
>
> after the sort, the index [0] binds to the '1' memory location and index
> [1] binds to '2'.
>
>
>
> Now if we have, d = {'a':1, 'b':2}
>>>> l = d.keys().sort()
>>>> print l
> None
>
>
> d.keys() is a list which references the keys of the dictionary.
> But the sort method does not do what is intended on this list?
>
> --Bhaskar.
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
--
I am but a man.
More information about the BangPypers
mailing list