Answers for "sorting list of dictionaries python"

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

sorting a dictionary in python

x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
{k: v for k, v in sorted(x.items(), key=lambda item: item[1])}
{0: 0, 2: 1, 1: 2, 4: 3, 3: 4}
Posted by: Guest on April-22-2020
1

sorting values in dictionary in python

#instead of using python inbuilt function we can it compute directly.
#here iam sorting the values in descending order..
d = {1: 1, 7: 2, 4: 2, 3: 1, 8: 1}
s=[]
for i in d.items():
  s.append(i)
for i in range(0,len(s)):
  for j in range(i+1,len(s)):
    if s[i][1]<s[j][1]:
      s[i],s[j]=s[j],s[i]
print(dict(s))
Posted by: Guest on February-14-2021

Code answers related to "sorting list of dictionaries python"

Python Answers by Framework

Browse Popular Code Answers by Language