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

Cliff Wells LogiplexSoftware at earthlink.net
Thu Jul 10 16:45:52 EDT 2003


On Thu, 2003-07-10 at 13:14, 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?

Because sort() sorts the list in-place.  It doesn't return anything
(which in Python means it returns None).

Try this instead:

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

-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726  (800) 735-0555






More information about the Python-list mailing list