tags:

views:

159

answers:

2
+1  Q: 

python chaining

Updated

Let's say I have:

dic={"z":"zv", "a":"av"}
## why doesn't the following return a sorted list of keys?
keys=dic.keys().sort()

I know I could do the following and have the proper result:

dic={"z":"zv", "a":"av"}
keys=dic.keys()
skeys=keys.sort()  ### skeys will be None

Why doesn't the first example work?

+10  A: 

sort() modifies the contents of the existing list. it doesn't return a list. See the manual.

AJ
should have read the fine print... thanks!
jldupont
The return value of `x.sort()` is always `None`. It updated `x`, it did not make a copy.
S.Lott
@jldupont, no worries...this one throws lots of folks for a loop.
AJ
would really appreciate some feedback on the sudden downvotes...what gives?
AJ
+12  A: 

.sort doesn't return the list. You could do:

keys = sorted(dic.keys())
Daniel Roseman
+1 for including a solution for the poster as well
Steven Hepting