1unsorted_list = [{"key1":5, "key2":2}, {"key1":5, "key2":1}]
2sorted_list = sorted(unsorted_list, key=lambda k: k["key2"])
1l = {1: 40, 2: 60, 3: 50, 4: 30, 5: 20}
2d1 = dict(sorted(l.items(),key=lambda x:x[1],reverse=True))
3print(d1) #output : {2: 60, 3: 50, 1: 40, 4: 30, 5: 20}
4d2 = dict(sorted(l.items(),key=lambda x:x[1],reverse=False))
5print(d2) #output : {5: 20, 4: 30, 1: 40, 3: 50, 2: 60}
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}
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])