0

I have 40 cases

cases = 40

I'm dealing with a period of 41 years:

years = 41

I have a list made of lists (a list for each case):

list = [[0] * years]*cases

and I want to loop over it to define cell by cell with an offset of 7 years :

for k in range(cases):
    for i in range(years):
            if int(i+7) < years:
                list[k][i+7] = random_number

I tried a code very similar to this one and it is not working as I thought it would. Basically, the final "k" loop (k = 39), will be the one repeated over all sublists.

Making it more simple:

IF I do this:

list[0][3] = 5

the fourth element of ALL sublists will be 5, not only the fourth element of the FIRST sublist. I don't want list[5][3] to be 5 as well. Because the result will be that the final loop will be the one repeated over the sublists.

How can I change that? Is it possible? I was able to use append to fix part of the problem, but since I need to add the offset (which is more complicated than 7 in the code that I'm dealing with, I can't use append because the order of addition is not linear.

Many thanks in advance

Paul M.
  • 10,481
  • 2
  • 9
  • 15
  • Is [this previous discussion](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) helpful to you? – motto Apr 10 '23 at 21:15

0 Answers0