Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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')]
Comment

PREVIOUS NEXT
Code Example
Python :: imbade image to jupyter notebook 
Python :: python remove read only file 
Python :: bar chart with seaborn 
Python :: how to make a discord bot dm someone python 
Python :: change background color of tkinter 
Python :: python format currency 
Python :: pytho list items to int 
Python :: import decisiontreeclassifier 
Python :: min max and avg function of python 
Python :: pandas shift one column 
Python :: selenium scroll element into view inside overflow python 
Python :: python year month day hour minute second 
Python :: how to minimize command console python 
Python :: django proper capitalization case jinja 
Python :: convert dataframe column to float 
Python :: _csv.Error: field larger than field limit (131072) 
Python :: python sys halt 
Python :: django filter not null 
Python :: split filename and extension python 
Python :: python degrees to radians 
Python :: from sklearn.preprocessing import standardscaler error 
Python :: pandas to csv encoding 
Python :: python nCr n choose r function 
Python :: input stdin python 
Python :: sklearn version 
Python :: AttributeError: This QueryDict instance is immutable django 
Python :: set seed python 
Python :: ubuntu cant find python installation 
Python :: python add unique to list 
Python :: lisy in python 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =