Method of sorted in python -
the same code, run several times, different results. why following results not sorted?
you put results dict
– data structure not guarantee order of keys/values. seems you're aware of that, judging call sorted()
.
however, function doesn't work presume: rather modifying list (or iterable, in general), returns new one. documentation linked above states:
return new sorted list items in iterable.
and since result of function isn't assigned variable, perishes. alternately, result can used directly collection iterate on in for
loop, this:
for key in sorted(scores): print(scores[key] + ' had ' + key)
Comments
Post a Comment