1s = {1: 1, 7: 2, 4: 2, 3: 1, 8: 1}
2k = dict(sorted(s.items(),key=lambda x:x[0],reverse = True))
3print(k)
1x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
2{k: v for k, v in sorted(x.items(), key=lambda item: item[1])}
3{0: 0, 2: 1, 1: 2, 4: 3, 3: 4}
1from operator import itemgetter
2new_dict = sorted(data.items(), key=itemgetter(1))
1#for dictionary d
2sorted(d.items(), key=lambda x: x[1]) #for inceasing order
3sorted(d.items(), key=lambda x: x[1], reverse=True) # for decreasing order
4#it will return list of key value pair tuples
1orders = {
2 'cappuccino': 54,
3 'latte': 56,
4 'espresso': 72,
5 'americano': 48,
6 'cortado': 41
7}
8
9sort_orders = sorted(orders.items(), key=lambda x: x[1], reverse=True)
10
11for i in sort_orders:
12 print(i[0], i[1])
1In [1]: import collections
2
3In [2]: d = {2:3, 1:89, 4:5, 3:0}
4
5In [3]: od = collections.OrderedDict(sorted(d.items()))
6
7In [4]: od
8Out[4]: OrderedDict([(1, 89), (2, 3), (3, 0), (4, 5)])