Python – sorting a list of dictionary values by date in python


I have a list and I am appending a dictionary to it as I loop through my data…and I would like to sort by one of the dictionary keys.


data = "data from database"
list = []
for x in data:
     dict = {'title':title, 'date': x.created_on}

I want to sort the list in reverse order by value of 'date'

Best Solution

You can do it this way:

list.sort(key=lambda item:item['date'], reverse=True)