Answers for "how to keep order in dictionary python"

3

sort dictionary python

l = {1: 40, 2: 60, 3: 50, 4: 30, 5: 20}
d1 = dict(sorted(l.items(),key=lambda x:x[1],reverse=True))
print(d1) #output : {2: 60, 3: 50, 1: 40, 4: 30, 5: 20}
d2 = dict(sorted(l.items(),key=lambda x:x[1],reverse=False))
print(d2) #output : {5: 20, 4: 30, 1: 40, 3: 50, 2: 60}
Posted by: Guest on September-10-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 "how to keep order in dictionary python"

Python Answers by Framework

Browse Popular Code Answers by Language