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

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.

ex:

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

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)