I have a list with repeated values and need to modify all of those. Notably, the repetitions always occur with a known fixed interval – for example, the 4'th and 5'th, the 9'th and 10'th and so on.
For example, the starting list might look like this:
my_list = [
'employee',
'company',
'value',
'profit',
'profit',
'employee',
'company',
'value',
'profit',
'profit',
]
Every time a pair of 'profit' items appears, I need to replace them with two new values – one with "12 months" and the other with "3 months" appended. So the output would be like this:
my_list = [
'employee',
'company',
'value',
'profit 12 months',
'profit 3 months',
'employee',
'company',
'value',
'profit 12 months',
'profit 3 months',
]
So, every 3 and 4 indexes, I want to rename the items in my list.
I know how to edit an item in a list by index, such as my_list[3] = 'profit 12 months'. But my list will be very long and doing this manually would take a lot of time.
How can I operate on every n'th index, especially when there is an initial offset to it?