Horje
sort python dictionary by date Code Example
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')]




Python

Related
python RuntimeWarning: overflow encountered in long_scalars Code Example python RuntimeWarning: overflow encountered in long_scalars Code Example
python how to listen to keyboard Code Example python how to listen to keyboard Code Example
python ubuntu check if a key is pressed Code Example python ubuntu check if a key is pressed Code Example
python get keypressed value Code Example python get keypressed value Code Example
module to read keyboard Code Example module to read keyboard Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7