Python chaining

Question:

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?

Asked By: jldupont

||

Answers:

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

Answered By: AJ.

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

keys = sorted(dic.keys())
Answered By: Daniel Roseman
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.