How would you order a dictionary according to its values or keys? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How would you order a dictionary according to its values or keys?

For example, if you have this: dictionary = {"a":5, "c":2, "b":10} What would you do to order the dictionary according to its values or keys and giving as a result a dictionary?

8th Feb 2020, 5:11 PM
Cristhian Rabi
Cristhian Rabi - avatar
4 Answers
+ 3
By key: for key in sorted(dictionary): print(dictionary[key]) By value: for key in sorted(d, key=lambda x: d[x]): print(d[key])
8th Feb 2020, 5:30 PM
HonFu
HonFu - avatar
+ 2
//You mean format in like ascending or descending order??
8th Feb 2020, 5:13 PM
Sudarshan Rai
Sudarshan Rai - avatar
+ 1
The MUST depend on application. The dict might show User:XP For a TopTenList u need order by value.
8th Feb 2020, 6:27 PM
Oma Falk
Oma Falk - avatar
0
yes, or in alphabetic order in the case of the keys
8th Feb 2020, 5:16 PM
Cristhian Rabi
Cristhian Rabi - avatar