Answers for "python sort a dictionary by key"

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
3

python sort dict by key

A={1:2, -1:4, 4:-20}
{k:A[k] for k in sorted(A)}

output:
{-1: 4, 1: 2, 4: -20}
Posted by: Guest on October-15-2020
0

how to sort a dictionary

markdict = {"Tom":67, "Tina": 54, "Akbar": 87, "Kane": 43, "Divya":73}
marklist = sorted(markdict.items(), key=lambda x:x[1])
sortdict = dict(marklist)
print(sortdict)
Posted by: Guest on May-24-2021
0

sort dictionary by key

dictionary_items = a_dictionary.items()
sorted_items = sorted(dictionary_items)
Posted by: Guest on June-08-2020

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

Python Answers by Framework

Browse Popular Code Answers by Language