MikeVaughan
MikeVaughan

Reputation: 1501

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 go about converting them into DateTime objects or otherwise sorting them?

Upvotes: 3

Views: 108

Answers (1)

Mureinik
Mureinik

Reputation: 311393

You could 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'))

Upvotes: 6

Related Questions