Generate a range of dates between start_date and end_date in pure Python using a generator or list comprehension with datetime.date and datetime.timedelta(days=1)—specifically [start + timedelta(days=i) for i in range((end - start).days + 1)] for an inclusive range—or call pd.date_range(start, end, freq="D") when working in Pandas or needing business days (freq="B"), month ends (freq="ME"), or hourly intervals (freq="h").
Basic Example
from datetime import date, timedelta
import pandas as pd
start_date = date(2026, 11, 25)
end_date = date(2026, 11, 30)
# 1. Recommended (Pure Python, inclusive): List comprehension with `timedelta`
num_days = (end_date - start_date).days
date_list = [start_date + timedelta(days=i) for i in range(num_days + 1)]
print(date_list)
# Output: [datetime.date(2026, 11, 25), datetime.date(2026, 11, 26), ..., datetime.date(2026, 11, 30)]
# 2. Memory-efficient lazy generator for huge multi-year date spans:
def iter_dates(start: date, end: date, step_days: int = 1):
current = start
step = timedelta(days=step_days)
while current <= end:
yield current
current += step
for d in iter_dates(start_date, end_date, step_days=2):
print(d.isoformat())
# Output: 2026-11-25, 2026-11-27, 2026-11-29
# 3. Recommended for DataFrames & custom frequencies (`pd.date_range`):
pd_dates = pd.date_range(start="2026-11-25", end="2026-11-30", freq="D")
print(pd_dates.strftime("%Y-%m-%d").tolist())
# Output: ['2026-11-25', '2026-11-26', '2026-11-27', '2026-11-28', '2026-11-29', '2026-11-30']
Key Details & Edge Cases
- Inclusive (
+ 1) vs. half-open ([start, end)) boundaries:(end_date - start_date).dayscomputes the difference, sorange((end - start).days + 1)includesend_date(matchingpd.date_range(..., inclusive="both")). Omit+ 1(or passinclusive="left"in Pandas) when you want a half-open interval[start_date, end_date).
- Reversed ranges (
start_date > end_date):- When
start_date > end_date,(end_date - start_date).daysis negative, sorange(num_days + 1)cleanly returns an empty list[]. To step backward fromstart_datedown toend_date, userange(0, (end_date - start_date).days - 1, -1).
- When
- Prefer
datetime.dateoverdatetime.datetimefor calendar dates to avoid DST bugs:- Adding
timedelta(days=1)on timezone-awaredatetimeobjects across a Daylight Saving Time transition can shift local wall-clock hours (00:00$\rightarrow$01:00or23:00). Using puredatetime.dateobjects avoids DST shifts completely for calendar-day iteration.
- Adding
- Month-by-month or year-by-year steps (
timedeltadoes NOT acceptmonths=oryears=):datetime.timedeltasupports at mostweeks=,days=,hours=,minutes=, andseconds=. For monthly or yearly date ranges, usepd.date_range(start, end, freq="MS")(month start) /freq="ME"(month end) ordateutil.rrule.rrule(MONTHLY, dtstart=start, until=end).
- NumPy
np.arangealternative (dtype="datetime64[D]"):- When building vectorized NumPy arrays without Pandas,
np.arange("2026-11-25", "2026-12-01", dtype="datetime64[D]")generates contiguous 64-bit date arrays in C (note:np.arangeis exclusive of the stop date, so passend + 1 day).
- When building vectorized NumPy arrays without Pandas,
Comparison of Date-Range Generation Methods in Python
| Method | Inclusive of end by Default? |
Supports Business Days / Months / Hours? | Dependencies | Best Used When |
|---|---|---|---|---|
[start + timedelta(days=i) for i in range((end - start).days + 1)] (Recommended) |
Yes (with + 1) |
Fixed day/week/hour steps only | None (standard library) | Generating standard calendar dates in zero-dependency Python scripts and APIs. |
iter_dates(start, end) generator |
Yes (while current <= end) |
Fixed timedelta steps |
None (standard library) | Streaming decades/centuries of dates without allocating a full list in memory. |
pd.date_range(start, end, freq="D") (Recommended for analytics) |
Yes (configurable via inclusive=) |
Yes ("D", "B", "W", "MS", "ME", "h", "15min") |
pandas |
Time-series indexing, resampling, business-day calendars, and DataFrame reindexing. |
np.arange(start, end_plus_1, dtype="datetime64[D]") |
No (half-open [start, stop)) |
Fixed calendar/time units ([D], [W], [M], [h]) |
numpy |
Fast vectorized NumPy date arrays without importing Pandas. |
You can generate a range of dates in Python using the built-in
datetimemodule.Example
from datetime import date, timedelta start_date = date(2026, 11, 25) end_date = date(2026, 11, 30) current_date = start_date while current_date <= end_date: print(current_date) current_date += timedelta(days=1)Output:
Using Pandas
You can also use
pd.date_range()if you are working with Pandas.import pandas as pd dates = pd.date_range("2026-11-25", "2026-11-30") print(dates)Explanation:
start_datedefines the first date.end_datedefines the last date.timedelta(days=1)moves to the next day.pd.date_range()generates dates automatically.Note: Both methods include the start and end dates.