Python find the max date in a list which are no later than a given date -


i want compare date string list of date strings, , find out max date in list no later date given.

date='2015-04-16'  dates=[str(int(date[:4])-1)+'-05-01'     ,str(int(date[:4])-1)+'-09-01'     ,str(int(date[:4])-1)+'-11-01'     ,date[:4]+'-05-01'     ,date[:4]+'-09-01'     ,date[:4]+'-11-01'] 

the right output should third element in dates, '2014-11-01'.

can me this? thanks!

since you've used lexicographically-ordered date format, can compare strings directly. finding maximum of group of items job of max(). choosing items go in group can done generator expression.

date='2015-04-16'  dates=[str(int(date[:4])-1)+'-05-01'     ,str(int(date[:4])-1)+'-09-01'     ,str(int(date[:4])-1)+'-11-01'     ,date[:4]+'-05-01'     ,date[:4]+'-09-01'     ,date[:4]+'-11-01']  print max(x x in dates if x < date) 

references: