sort() doesn't work on dist.keys() ?

Greg Fortune lists at gregfortune.com
Fri Jul 11 09:31:23 EDT 2003


Because sorts works in place and the sort fuction returns None rather than a
copy of the list.  Try


import sys
temp = sys.modules.keys()
temp.sort()
print temp


Greg


Steve Pinard wrote:

> (Got a comm error trying to post first time, sorry if this
> is a duplicate)
> 
> New to Python, so please bear with me.
> 
>>>> import sys
>>>> print sys.modules.keys()          # works fine
> ['code', ...snip... ]
>>>> print sys.modules.keys().sort()   # returns None, why?
> None
> 
> According to my reference (Nutshell), keys() returns a
> "copy" of the dict keys as a list, so I would expect when
> I aply sort() to that list, I would get an in-place sorted
> version of that list.  Why do I get None?
> 
> TIA,
> - Steve





More information about the Python-list mailing list