Answers for "sort by key in dictionary python"

35

how can I sort a dictionary in python according to its values?

s = {1: 1, 7: 2, 4: 2, 3: 1, 8: 1}
k = dict(sorted(s.items(),key=lambda x:x[0],reverse = True))
print(k)
Posted by: Guest on November-23-2020
14

sort list of dictionaries by key python

newlist = sorted(list_to_be_sorted, key=lambda k: k['name'])
Posted by: Guest on March-25-2020
12

how to sort a dictionary by value in python

import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(1))


# Sort by key
import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(0))
Posted by: Guest on November-27-2019
2

sort a dictionary

from operator import itemgetter
new_dict = sorted(data.items(), key=itemgetter(1))
Posted by: Guest on September-24-2020
0

how to sort a dictionary py

orders = {
	'cappuccino': 54,
	'latte': 56,
	'espresso': 72,
	'americano': 48,
	'cortado': 41
}

sort_orders = sorted(orders.items(), key=lambda x: x[1], reverse=True)

for i in sort_orders:
	print(i[0], i[1])
Posted by: Guest on May-18-2021
0

python dict order a dict by key

d1 = dict(sorted(d.items(), key = lambda x:x[0]))
Posted by: Guest on October-20-2021

Code answers related to "sort by key in dictionary python"

Python Answers by Framework

Browse Popular Code Answers by Language