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
Python | Consecutive elements pairing in list
This process involves creating pairs of elements that appear next to each other, which can be invaluable for various applications such as data analysis, pattern recognition and algorithm development. We can achieve this using different methods in Python, such as using simple loops, list slicing, zip
3 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 - Grouped Consecutive Range Indices of Elements
Given List of elements, for list of tuples, where each represents the continuity of occurrence of each element. Input : test_list = [1, 1, 5, 6, 5, 5] Output : {1: [(0, 1)], 5: [(2, 2), (4, 5)], 6: [(3, 3)]} Explanation : 5 present at 2nd idx and also in continuation in 4th and 5th index, and hence
8 min read
Python - Consecutive Kth column Difference in Tuple List
Sometimes, while working with Python list, we can have a task in which we need to work with tuple list and get the absolute difference of itâs Kth index. This problem has application in web development domain while working with data informations. Letâs discuss certain ways in which this task can be
6 min read
Python - Consecutive Ranges of K greater than N
Given a list of elements, the task is to write a Python program to get all ranges of K greater than N. Input : [2, 6, 6, 6, 6, 5, 4, 6, 6, 8, 4, 6, 6, 6, 2, 6], K = 6, N = 3 Output : [(1, 4), (11, 13)] Explanation : 6 is consecutive from index 1 to 4, hence 1-4 in result. 7-8 also has 6, but its les
4 min read
Python - Consecutive identical elements count
Given the elements list, get all the elements that have an identical element as the next element. Input : test_list = [4, 5, 5, 5, 5, 6, 6, 7, 8, 2, 2, 10] Output : 3 Explanation : 5, 6 and 2 has identical element as their next element. Input : test_list = [4, 5, 5, 5, 5, 6, 6, 7, 8, 2, 3, 10] Outpu
5 min read
Python | Find missing elements in List
Sometimes, we can get elements in range as input but some values are missing in otherwise consecutive range. We might have a use case in which we need to get all the missing elements. Let's discuss certain ways in which this can be done. Method #1 : Using list comprehension We can perform the task o
7 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