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

S

Steve Pinard

(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
 
G

Greg Krohn

Steve Pinard said:
(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
 
T

Troels Therkelsen

(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?

From the documentation of the mutable sequence sort() method, note (7):

"The sort() and reverse() methods modify the list in place for
economy of space when sorting or reversing a large list. To remind
you that they operate by side effect, they don't return the sorted
or reversed list."

Or, in other words, sort() always returns None. If you want to sort, you
need to bind a name to the list you want to sort, first, then call sort() on
it and then print the now sorted list. For example:

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

Hope this helps,

Troels Therkelsen
 
C

Cliff Wells

(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
 
G

Greg Fortune

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 said:
(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top