How to loop through a list with a set amount of positions and start at the beginning when you reach the end

Question:

as the title says i need to loop through a list that contains 7 values but i need to start at the beginning each cycle in order to get the right value based off of another value that my code gets, also needs to be done with pure python. I know that there are libraries to do it but i want to not use them. so the idea is that i have a list of the days of the week and whatever value i get for how many days later the time is i need to get the specific day of the week

Asked By: jiriki79

||

Answers:

As suggested by one of the comments, you could use the modulo (%) operator, used to get the remainder of a number divided by another.

It is especially useful to continuously "roll over" an increased index value back from the beginning (or a decreased index back towards the end) when it goes outside the range.

# List of the days of the week
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
# The total number of days
n_days = len(days)

# Start at a certain day (in this case, Sunday)
start_day = "Sunday"

# Get the corresponding index
start_index = days.index(start_day)

# The number of days later
n = 20

# Get the index of the day of the week n days later:
d_index = (start_index + 20) % n_days

print("Start day:", start_day)
print(n, "days later:", days[d_index])
Start day: Sunday
20 days later: Saturday

But if every step matters, then a loop would be necessary:

days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
n_days = len(days)

start_day = "Sunday"
start_index = days.index(start_day)
n = 20

print("Start day:", start_day)
print("nNext", n, "days:")

# Iterate over the next n days
for i in range(1, n + 1):
    # Get the index corresponding to the current day.
    d_index = (start_index + i) % n_days
    print(i, days[d_index])
Start day: Sunday

Next 20 days:
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday
8 Monday
9 Tuesday
10 Wednesday
11 Thursday
12 Friday
13 Saturday
14 Sunday
15 Monday
16 Tuesday
17 Wednesday
18 Thursday
19 Friday
20 Saturday

Written as functions

days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
n_days = len(days_of_week)

# Outputs the day of the week n days after one given.
def advance_day(start_day, n=1):
    start_index = days_of_week.index(start_day)
    d_index = (start_index + n) % n_days

    return days_of_week[d_index]

# Outputs a list of the next n days after the one given.
def next_n_days(start_day, n):
    start_index = days_of_week.index(start_day)
    
    next_days = []
    for i in range(1, n + 1):
        d_index = (start_index + i) % n_days
        next_days.append(days_of_week[d_index])

    return next_days

Usage:

# Get the day of the week 20 days after Sunday.
advance_day("Sunday", 20)
# Get the next 20 days after Sunday (as a list).
next_n_days("Sunday", 20)
Answered By: user21283023
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.