Convert standard format date string to datetime object

I have a list of tuples, each containing a date and then a number.

days = [('04/02/15', 4.5),('03/15/15', 5.0),('04/21/15', 1.9)]

      

I want to sort them by date. How do I convert them to DateTime objects or otherwise sort them?

+3


source to share


1 answer


You can use strptime

:



from time import strptime
days = [('04/02/15', 4.5), ('03/15/15', 5.0), ('04/21/15', 1.9)]
days.sort(key = lambda tup: strptime(tup[0], '%m/%d/%y'))

      

+6


source







All Articles