How do I sort a list of dictionaries by a value of the dictionary?

后端 未结 18 2792
半阙折子戏
半阙折子戏 2020-11-21 04:06

I have a list of dictionaries and want each item to be sorted by a specific value.

Take into consideration the list:

[{\'name\':\'Homer\', \'age\':39},         


        
18条回答
  •  再見小時候
    2020-11-21 05:03

    You may also use to sort by second element (x[1])
    of dct_name.items() and call the dict() constructor to get it as a dictionary

    sorted_dct = dict(sorted(dct_name.items(), key = lambda x : x[1]))
    

提交回复
热议问题