Creating a range of dates in Python
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Python does not have a built-in daterange() function like range() for integers. To generate a sequence of dates, use datetime.timedelta in a loop, a generator function, or pandas.date_range() for data analysis workflows. Each approach lets you iterate over consecutive days, weeks, months, or custom intervals.
Method 1: timedelta Loop
Method 2: Generator Function (Recommended)
The generator yields one date at a time without building the full list in memory.
Method 3: List Comprehension
Method 4: pandas.date_range()
Common Frequency Codes
| Code | Meaning |
D | Calendar day |
B | Business day (Mon-Fri) |
W | Weekly (default Sunday) |
W-MON | Weekly on Monday |
MS | Month start |
ME | Month end |
QS | Quarter start |
YS | Year start |
h | Hourly |
Method 5: dateutil.rrule
dateutil.rrule is especially useful for month and year intervals where month lengths vary.
Working with datetime (Including Time)
Business Days Only
Formatting Date Ranges
Reverse Range (Descending)
Common Pitfalls
- Off-by-one on end date:
range(10)excludes 10. Be explicit about whether your date range includes the end date. Use<=for inclusive,<for exclusive. - Month arithmetic with timedelta:
timedelta(days=30)does not equal "one month" — months have 28-31 days. Usedateutil.relativedelta(months=1)for month-accurate arithmetic. - Timezone-naive date ranges:
dateobjects have no timezone. If you need timezone-aware ranges, usedatetimewithzoneinfo.ZoneInfoand check DST transitions. - Memory with large ranges:
[start + timedelta(days=i) for i in range(365*100)]creates a list of 36,500 dates in memory. Use a generator for large ranges. - pandas freq string changes: In pandas 2.x, some frequency strings changed (e.g.,
MbecameMEfor month-end,YbecameYEfor year-end). Check your pandas version.
Summary
- Use a generator with
timedelta(days=1)for simple day-by-day iteration - Use list comprehension
[start + timedelta(days=i) for i in range(n)]for small ranges - Use
pandas.date_range()for data analysis with flexible frequency codes - Use
dateutil.rrulefor month/year intervals that respect varying month lengths - Use
pd.bdate_range()for business-day-only ranges - Always be explicit about inclusive vs exclusive end dates
Related reading
- Creating a simple XML file using python
- creating a spiral array in python?
- Creating a tensorflow dataset that outputs a dict
- Creating an empty list in Python
- Creating an empty Pandas DataFrame, and then filling it
- Creating functions or lambdas in a loop or comprehension
- Creating Threads in python
- CRITICAL WORKER TIMEOUT on gunicorn when deployed to AWS
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.