I have a list like ['Monday', 'Thursday'] with two days of the week. I want to be able to retrieve a list of days of the week between these two -> ['Monday', 'Tuesday', 'Wednesday', 'Thursday']
-
I am happy to write a custom function in Python to do this. But wondering if there is an easier way that I might be missing.
-
How can I retrieve the day index for a given day of the week (day of the week name – example:’Wednesday’) in Python?
>Solution :
To retrieve a list of days of the week between two given days, you can use below function:
def days_between(start_day, end_day):
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
start_index = days.index(start_day)
end_index = days.index(end_day)
if start_index <= end_index:
return days[start_index:end_index+1]
else:
return days[start_index:] + days[:end_index+1]
# Example usage
start_day = 'Monday'
end_day = 'Thursday'
result = days_between(start_day, end_day)
print(result)