Why (dictionary.keys ()). Sort () not working in python?

I am new to Python and don't understand why such a thing does not work. I can not find the problem raised elsewhere.

toto = {'a':1, 'c':2 , 'b':3}
toto.keys().sort()           #does not work (yields none)
(toto.keys()).sort()         #does not work (yields none)
eval('toto.keys()').sort()   #does not work (yields none)

But if I check the type, I see that I am calling sort () on the list, so what is the problem.

toto.keys().__class__     # yields <type 'list'>

The only way I have to work is to add a temporary variable that is ugly

temp = toto.keys()
temp.sort()

What I'm missing here should be the best way to do this.

+3
source share
3 answers

sort()sorts the list in place. It returns Noneso you don’t think that it will leave the original list alone and will return a sorted copy.

+7
source
sorted(toto.keys())

, . , , None.

+7

sort() , none. sorted(toto.keys()), , .

+1
source

Source: https://habr.com/ru/post/1730721/


All Articles