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)
Written by Russell Hay
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#