Python: Create List of Days Between Two Dates

πŸ’‘ Problem Formulation: You’re tasked with generating a sequence of dates in Python, representing all days between two given dates inclusively. For example, given a start date of 2023-01-01 and an end date of 2023-01-05, your script should be able to produce a list of dates that include January 1, 2, 3, 4, and 5 of the year 2023.

Method 1: Using datetime and a Loop

This method entails utilizing the datetime module, which is a part of Python’s standard library. The approach increments the start date by one day in a loop until it reaches the end date. We’ll be using datetime.timedelta objects to represent the difference between two datetime.date objects.

Here’s an example:

from datetime import datetime, timedelta

start_date = datetime.strptime('2023-01-01', '%Y-%m-%d').date()
end_date = datetime.strptime('2023-01-05', '%Y-%m-%d').date()

current_date = start_date
dates_list = []

while current_date <= end_date:
    dates_list.append(current_date)
    current_date += timedelta(days=1)

print(dates_list)

In this example, we parse the start and end dates into date objects and store the sequence in dates_list. We then loop from the start date to the end date, inclusive, by incrementally adding one day (timedelta(days=1)) until the loop condition is not satisfied.

You can output the dates like so:

for date in dates_list:
    print(str(date))

The output in my example would be:

2023-01-01
2023-01-02
2023-01-03
2023-01-04
2023-01-05

Method 2: Using pandas.date_range

Pandas is a powerful data manipulation library that simplifies working with dates and times. The pandas.date_range function can generate date ranges automatically, providing a very concise way to solve our problem.

Here’s an example:

import pandas as pd

start_date = '2023-01-01'
end_date = '2023-01-05'

dates_list = pd.date_range(start=start_date, end=end_date).tolist()

print(dates_list)

In the code snippet, pandas.date_range is used to create a DateTimeIndex that spans from the start date to the end date. The .tolist() method converts this to a list of Timestamp objects, which we print out. This method is very concise and powerful, especially for those already using Pandas for data analysis.

Method 3: Using List Comprehension with datetime

List comprehension in Python offers a concise syntax for creating lists. Employing list comprehension alongside the datetime module can give us a one-liner solution to generate our list of dates.

Here’s an example:

from datetime import datetime, timedelta

start_date = datetime.strptime('2023-01-01', '%Y-%m-%d').date()
end_date = datetime.strptime('2023-01-05', '%Y-%m-%d').date()

dates_list = [(start_date + timedelta(days=x)) for x in range((end_date - start_date).days + 1)]

print(dates_list)

What the above code does is calculate the difference between end_date and start_date to find the number of days in the range, then iterates over this range, adding each day to the start date. The result is a list of datetime.date objects, representing each day in the specified date range.

Method 4: Using iter with a Custom Generator Function

Creating a custom generator function allows you to use iter to produce an iterator that yields dates from the start date to the end date. This method offers a clear and pythonic approach to the problem.

Here’s an example:

from datetime import datetime, timedelta

def daterange(start_date, end_date):
    for n in range(int((end_date - start_date).days) + 1):
        yield start_date + timedelta(n)

start_date = datetime.strptime('2023-01-01', '%Y-%m-%d').date()
end_date = datetime.strptime('2023-01-05', '%Y-%m-%d').date()

dates_list = list(iter(daterange(start_date, end_date)))

print(dates_list)

In this snippet, daterange is a generator function that receives a start and an end date. I then iteratively yields each day. Using the list() function, we convert the iterable into a list.

Bonus One-Liner Method 5: Using numpy.arange()

Numpy offers efficient and fast array operations. Its arange() function, when combined with numpy.datetime64, lets you handle dates and can thus be used to create a list of days between two dates.

Here’s an example:

import numpy as np

start_date = '2023-01-01'
end_date = '2023-01-05'

dates_list = list(np.arange(np.datetime64(start_date), np.datetime64(end_date) + np.timedelta64(1, 'D'), dtype='datetime64[D]'))

print(dates_list)

This one-liner utilises Numpy’s arange function to produce a range of dates from start_date to end_date, both expressed as numpy.datetime64 objects. The resulting array is converted to a list and outputted.

Here’s a visual representation of how the function works on a high level:

Summary/Discussion

  • Method 1: Simple and doesn’t require any external libraries, but is not the most efficient for large ranges.
  • Method 2: Very concise if pandas is already a project dependency, not ideal if you want to avoid an extra dependency.
  • Method 3: Suitable for those who prefer one-liners while still using Python’s standard library, offers list comprehension readability.
  • Method 4: Highly readable, good for large date ranges due to the use of a generator function, but slightly more complex.
  • Bonus Method 5: Extremely fast operation for large date ranges and a succinct one-liner, however, comes with the overhead of requiring numpy, which may not be necessary if you’re not performing numerical operations.

Check out my new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Publisher Link: https://nostarch.com/pythononeliners