sort list of dictionaries by key python
newlist = sorted(list_to_be_sorted, key=lambda k: k['name'])
sort list of dictionaries by key python
newlist = sorted(list_to_be_sorted, key=lambda k: k['name'])
sort python dictionary by date
from datetime import datetime
from collections import OrderedDict # For python 2.7 and above
data = { "01-01-2015" : "some data",
"05-05-2015" : "some data",
"03-04-2015" : "some data" }
# For python 2.7 and above:
ordered_data = OrderedDict(
sorted(data.items(), key = lambda x:datetime.strptime(x[0], '%d-%m-%Y')) )
# For python 2.6 and below where OrderedDict is not present:
# ordered_data = sorted(data.items(), key = lambda x:datetime.strptime(x[0], '%d-%m-%Y'))
print(ordered_data)
#Output :
[('01-01-2015', 'some data'),
('03-04-2015', 'some data'),
('05-05-2015', 'some data')]
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us