How to define an element in a list of lists to be equal to something

29 Views Asked by At

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

0

There are 0 best solutions below