0

here's my problem:

Let's say I build a 3x4 matrix:

The first way :

M = []
k=1;
while k<12:
M.append([k,k+1,k+2,k+3])
k=k+4

I get this in output:

0 -> [1, 2, 3, 4]                                                                                                                      
1 -> [5, 6, 7, 8]                                                                                                                      
2 -> [9, 10, 11, 12]

Ok great now let's assign some element withM[2][1]=0

0 -> [1, 2, 3, 4]                                                                                                                      
1 -> [5, 6, 7, 8]                                                                                                                      
2 -> [9, 0, 11, 12] 

OK it works. Now here's the issue with the second way :

    lst1=[] 
    T=[] 
    for k in range(0,3):
        lst1.append(0)
    for j in range(0,4):
        T.append(lst1)

output: [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] note: The code I have to do is a transpose function hence the dimensions of this matrix

Now let's (try to) assign T[3][0]=4:

output :

0 -> [4, 0, 0]                                                                                                                         
1 -> [4, 0, 0]                                                                                                                         
2 -> [4, 0, 0]                                                                                                                         
3 -> [4, 0, 0]

Nope it changes all the colum instead of the element. Is there a way to make it work properly on the second option ?

I don't understand why it does that. I use python3

Thanks ! A python newbie

  • 3
    My guess is, since you're assigning the same list to all the rows, it uses the same reference. Hence, when you change value in either one of them, it reflects it back in all of them. – Nitin Pawar Dec 10 '18 at 06:03
  • @NitinPawar more than a guess, it's exactly the problem. Can be circumvented using `T.append(lst1.copy())` – Julien Dec 10 '18 at 06:04
  • @schwobaseggl that is not the best duplicate target – juanpa.arrivillaga Dec 10 '18 at 06:04
  • Because you keep appending the *same list*: T.append(lst1) In every iteration of the loop. In essense, the resulting list is: [list1, list1, list1, list1] – juanpa.arrivillaga Dec 10 '18 at 06:05
  • You can confirm the above with `T[0] is T[1]` (== True). Good explanations [here](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – Mark Dec 10 '18 at 06:05
  • Thanks for your answers, especially@Julien for your workaround ! it works now . But it still weird to me that print(T[3][0]) shows 4 and T[3][0]=4 assign all the column – Yassine Jiyar Dec 10 '18 at 06:13
  • @juanpa.arrivillaga It is about building a nested list and subsequent changes being reflected in all sublists. Feels right to me. – user2390182 Dec 10 '18 at 06:19

0 Answers0