Python - Find consecutive dates in a list of dates
Last Updated :
12 Apr, 2023
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)]
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() + loop
from datetime import datetime, timedelta
# initializing list
test_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 list
print("The original list is : " + str(test_list))
# using loop for iterating all elements
res = True
for 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 result
print("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
Time Complexity : O(n)
Space Complexity : O(1)
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 list
test_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 list
print("The original list is : " + str(test_list))
# using loop for iterating all elements
res = all((test_list[idx] - test_list[idx - 1]).days ==
1 for idx in range(1, len(test_list)))
# printing result
print("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
Time Complexity : O(n)
Space Complexity : O(1)
Method #3: Using set()
- Initialize a new list named 'date_range' containing all dates from the first date in the 'test_list' to the last date in the 'test_list'. To do this, use the date range function and add the time delta of one day to each date.
- Check if 'date_range' and 'test_list' contain the same dates by converting both lists to sets and comparing them. If they are the same, then the dates in 'test_list' are consecutive.
- Return True if the dates are consecutive and False otherwise.
Python3
from datetime import datetime, timedelta
# initializing list
test_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 list
print("The original list is : " + str(test_list))
# using set() to check for consecutive dates
date_range = [test_list[0] + timedelta(days=x)
for x in range((test_list[-1]-test_list[0]).days + 1)]
res = set(test_list) == set(date_range)
# printing result
print("Are dates consecutive : " + str(res))
OutputThe 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
Time Complexity: O(n) - linear time complexity since we loop through the test_list and the date_range only once
Auxiliary Space: O(n) - linear space complexity since we create a new list, 'date_range', containing all dates in the given range
Similar Reads
Python - Check if list contains consecutive Checking if a list contains consecutive numbers in Python is useful in problems involving sequences, patterns, or data validation. In this article, we explore different methods to check for consecutive numbers in a list.Using sorted() and RangeThis method works by sorting the list and comparing it t
2 min read
Python - Find the closest date from a List Given a date and list of dates, the task is to write a python program to find the nearest date in the given input list of dates to the input date. Examples: Input : test_date_list = [datetime(2020, 4, 8), datetime(2016, 8, 18), datetime(2018, 9, 24), datetime(2019, 6, 10), datetime(2021, 8, 10)], te
4 min read
Creating a list of range of dates in Python Given a date, the task is to write a Python program to create a list of a range of dates with the next k dates starting from the current date. For example, if the given date is 4th January 1997 and k is 5, the output should be a list containing 4th January 1997, 5th January 1997, 6th January 1997, 7
3 min read
Python - Filter consecutive elements Tuples Given a Tuple list, filter tuples that are made from consecutive elements, i.e diff is 1. Input : test_list = [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 4), (6, 4, 6, 3)] Output : [(3, 4, 5, 6)] Explanation : Only 1 tuple adheres to condition. Input : test_list = [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 3), (6,
5 min read
Python - K difference Consecutive elements Given a list of integer elements, check for each element if its difference with successive element is K. Input : test_list = [5, 6, 3, 2, 5, 3, 4], K = 1 Output : [True, False, True, False, False, True] Explanation : 5, 6; 3, 2; and 3, 4 have 1 diff. between them. Input : test_list = [5, 6, 3, 2, 5,
4 min read
Python | Consecutive Maximum Occurrence in list Sometimes, while working with Python lists or in competitive programming setup, we can come across a subproblem in which we need to get an element which has the maximum consecutive occurrence. The knowledge of the solution of it can be of great help and can be employed whenever required. Let's discu
5 min read