Python – Find consecutive dates in a list of dates
Given a list of dates, the task is to write a Python program to check if all the dates are consecutive in the list.
Input : [datetime(2019, 12, 30), datetime(2019, 12, 31), datetime(2020, 1, 1), datetime(2020, 1, 2), datetime(2020, 1, 3), datetime(2020, 1, 4)]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Output : True
Explanation : All dates are consecutive, from 30 Dec 2019 to 4 January 2020.
Input : [datetime(2019, 12, 29), datetime(2019, 12, 31), datetime(2020, 1, 1), datetime(2020, 1, 2), datetime(2020, 1, 3), datetime(2020, 1, 4)]
Output : False
Explanation : Non consecutive dates.
Method #1 : Using days() + loop
In this, we check consecutive dates by checking days difference from the previous date using days(). The iteration of all dates is done using a loop.
Python3
# Python3 code to demonstrate working of# Test if dates are consecutive# Using days() + loopfrom datetime import datetime, timedelta # initializing listtest_list = [datetime(2019, 12, 30), datetime(2019, 12, 31), datetime(2020, 1, 1), datetime(2020, 1, 2), datetime(2020, 1, 3), datetime(2020, 1, 4)] # printing original listprint("The original list is : " + str(test_list)) # using loop for iterating all elementsres = Truefor idx in range(1, len(test_list)): # checking for 1 day time difference if (test_list[idx] - test_list[idx - 1]).days != 1: res = False break # printing resultprint("Are dates consecutive : " + str(res)) |
Output:
The original list is : [datetime.datetime(2019, 12, 30, 0, 0), datetime.datetime(2019, 12, 31, 0, 0), datetime.datetime(2020, 1, 1, 0, 0), datetime.datetime(2020, 1, 2, 0, 0), datetime.datetime(2020, 1, 3, 0, 0), datetime.datetime(2020, 1, 4, 0, 0)]
Are dates consecutive : True
Method #2 : Using all() + days()
Similar to the above method, the only difference here is all() is used to check for each day consecution for a more compact solution.
Python3
# Python3 code to demonstrate working of# Test if dates are consecutive# Using all() + days()from datetime import datetime, timedelta # initializing listtest_list = [datetime(2019, 12, 30), datetime(2019, 12, 31), datetime(2020, 1, 1), datetime(2020, 1, 2), datetime(2020, 1, 3), datetime(2020, 1, 4)] # printing original listprint("The original list is : " + str(test_list)) # using loop for iterating all elementsres = all((test_list[idx] - test_list[idx - 1]).days == 1 for idx in range(1, len(test_list))) # printing resultprint("Are dates consecutive : " + str(res)) |
Output:
The original list is : [datetime.datetime(2019, 12, 30, 0, 0), datetime.datetime(2019, 12, 31, 0, 0), datetime.datetime(2020, 1, 1, 0, 0), datetime.datetime(2020, 1, 2, 0, 0), datetime.datetime(2020, 1, 3, 0, 0), datetime.datetime(2020, 1, 4, 0, 0)]
Are dates consecutive : True

