Last Updated: February 25, 2016
·
1.059K
· russtheaerialist

Find the last consecutive date in a list

A simple way to find the last consecutive date in a list.

def last_consecutive(x, y):
    return x if y-x>datetime.timedelta(days=1) else y

reduce(last_consecutive, date_list)

So if you have a list of the following:

date_list = [
    date(2014, 1, 1),
    date(2014, 1, 2),
    date(2014, 1, 3),
    date(2014, 1, 10)
]

The reduce call would return date(2014, 1, 3)