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

Greg Krohn ask at me.com
Thu Jul 10 16:24:15 EDT 2003


"Steve Pinard" <spinard at ra.rockwell.com> wrote in message
news:6cd58b6.0307101214.34e99f2a at posting.google.com...
> (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
>

You said it yourself. It's an IN PLACE sort, so it won't return anything
(well, None, but that doesn't count). Try this:

**UNTESTED**
>>> mykeys = sys.modules.keys()
>>> print mykeys
[...a list of keys...]
>>> mykeys.sort() #Note it's IN PLACE. Nothing is returned, so there's no
need to assign anything
>>> print mykeys
[...a SORTED list of keys...]

Greg






More information about the Python-list mailing list