Creating a list of range of dates in Python
Last Updated :
12 Apr, 2025
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, 7th January 1997 and 8th January 1997. This can be achieved using various methods available in Python.
Using pd.date_range
In this method, we will use pandas date_range to create a list of ranges of dates in Python. It is ideal for users working with data analysis or time series. This method is clean, readable and leverages the powerful pandas library to handle date ranges efficiently.
Python
import datetime
import pandas as pd
td = datetime.datetime.strptime("01-7-2022", "%d-%m-%Y")
k = 5
d = pd.date_range(td, periods=k)
print(d.strftime("%d-%m-%Y"))
OutputIndex(['01-07-2022', '02-07-2022', '03-07-2022', '04-07-2022', '05-07-2022'], dtype='object')
Explanation: Generates a range of k consecutive dates from a given start date using Pandas' date_range, which returns a DatetimeIndex.
Using timedelta() + list comprehension
In this, we get to add consecutive deltas to day using timedelta() and list comprehension is used to iterate through the required size and construct the required result.
Python
import datetime
td = datetime.datetime(1997, 1, 4)
k = 5
res = [td + datetime.timedelta(days=idx) for idx in range(k)]
print("Next k dates list : " + str(res))
OutputNext k dates list : [datetime.datetime(1997, 1, 4, 0, 0), datetime.datetime(1997, 1, 5, 0, 0), datetime.datetime(1997, 1, 6, 0, 0), datetime.datetime(1997, 1, 7, 0, 0), datetime.datetime(1997, 1, 8, 0...
Explanation: This code generates a list of dates by adding timedelta(days=idx) to the starting date using list comprehension.
Using Python Loop
In this, we perform a similar task as the above function, using a generator to perform the task of Date successions.
Python
import datetime
td = datetime.date(2022,8,12)
k = 5
res = []
for day in range(k):
date = (td + datetime.timedelta(days = day)).isoformat()
res.append(date)
print("Next k dates list: " + str(res))
OutputNext k dates list: ['2022-08-12', '2022-08-13', '2022-08-14', '2022-08-15', '2022-08-16']
Explanation: Uses a simple loop to calculate each date, convert it to ISO string format and append it to a list.
Using while Loop with timedelta()
This method is best when both a start and end date are known. This method gives all dates within the range, regardless of how many days.
Python
from datetime import datetime, timedelta
sd = datetime(2022, 3, 1)
ed = datetime(2022, 3, 10)
d = []
while sd <= ed:
d.append(sd.strftime("%d-%m-%Y"))
sd += timedelta(days=1)
print(d)
Output['01-03-2022', '02-03-2022', '03-03-2022', '04-03-2022', '05-03-2022', '06-03-2022', '07-03-2022', '08-03-2022', '09-03-2022', '10-03-2022']
Explanation: This code generates all dates between two specified dates using a while loop and timedelta(days=1).
Using Generator with yield and timedelta()
This method is best for memory efficiency when generating a large number of dates. This method yields one date at a time instead of storing all in memory.
Python
import datetime
def date_range_4(td, k):
i = 0
while i < k:
yield td + datetime.timedelta(days=i)
i += 1
td = datetime.datetime(1997, 1, 4)
k = 5
d = list(date_range_4(td, k))
print(d)
Output[datetime.datetime(1997, 1, 4, 0, 0), datetime.datetime(1997, 1, 5, 0, 0), datetime.datetime(1997, 1, 6, 0, 0), datetime.datetime(1997, 1, 7, 0, 0), datetime.datetime(1997, 1, 8, 0, 0)]
Explanation: This code implements a generator that yields each date in the range one-by-one, making it suitable for large datasets or lazy evaluation.
Related Articles:
Similar Reads
Create List of Numbers with Given Range - Python The task of creating a list of numbers within a given range involves generating a sequence of integers that starts from a specified starting point and ends just before a given endpoint. For example, if the range is from 0 to 10, the resulting list would contain the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8
3 min read
Python | Sort list of dates given as strings To sort a list of dates given as strings in Python, we can convert the date strings to datetime objects for accurate comparison. Once converted, the list can be sorted using Python's built-in sorted() or list.sort() functions. This ensures the dates are sorted chronologically.Using pandas.to_datetim
2 min read
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)] Output : True Explanation : All dates a
4 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 Program to check date in date range Given a date list and date range, the task is to write a Python program to check whether any date exists in the list in a given range. Example: Input : test_list = [datetime(2019, 12, 30), datetime(2018, 4, 4), datetime(2016, 12, 21), datetime(2021, 2, 2), datetime(2020, 2, 3), datetime(2017, 1, 1)]
10 min read
Python - Group dates in K ranges Given a list of dates, group the dates in a successive day ranges from the initial date of the list. We will form a group of each successive range of K dates, starting from the smallest date. Input : test_list = [datetime(2020, 1, 4), datetime(2019, 12, 30), datetime(2020, 1, 7), datetime(2019, 12,
6 min read
Python program to find Last date of Month Given a datetime object, the task is to write a Python Program to compute the last date of datetime object Month. Examples: Input : test_date = datetime.datetime(2018, 6, 4) Output : 30 Explanation : April has 30 days, each year Input : test_date = datetime.datetime(2020, 2, 4) Output : 29 Explanati
3 min read
Python Program to Display Calendar of a Given Month Displaying a calendar for a given month is a common task in programming, and Python provides several methods to achieve this. In this article, we will explore different approaches to creating a Python program that displays the calendar of a specified month. Whether using built-in libraries or third-
4 min read
Python - Generate k random dates between two other dates Given two dates, the task is to write a Python program to get K dates randomly. Input : test_date1, test_date2 = date(2015, 6, 3), date(2015, 7, 1), K = 7 Output : [datetime.date(2015, 6, 18), datetime.date(2015, 6, 25), datetime.date(2015, 6, 29), datetime.date(2015, 6, 11), datetime.date(2015, 6,
4 min read
Python program to print calendar of given year Given a valid year as input, write a Python program to print the calendar of given year. In order to do this, we can simply use calendar module provided in Python. For more details about calendar module, refer this article. Python3 1== # Python program to print calendar for given year # importing ca
3 min read