Sorting List Of Dictionaries With List As Values Based On Second Item Of List Value In Dictionary
I've a list which contains dictionaries and in each dictionaries the values are also list like : a = [{'25.21' : [400,500] , '25.22' : [700,502]} , {'12.15' : [350,800] , '12.13'
Solution 1:
Using sorted
Demo:
a = [{"25.21" : [400,500] , "25.22" : [700,502]} , {"12.15" : [350,800] , "12.13" : [750,803]}]
print(sorted(a, key=lambda x: list(x.items())[1][1], reverse=True))
#print(sorted(a, key=lambda x: x.items()[1][1], reverse=True)) #python2
Output:
[{'12.15': [350, 800], '12.13': [750, 803]}, {'25.21': [400, 500], '25.22': [700, 502]}]
Post a Comment for "Sorting List Of Dictionaries With List As Values Based On Second Item Of List Value In Dictionary"