0

I'm working on a code that on a certain point fills a 2x9 matrix with values (the matrix is, as default, filled with None values).

for i in range(1, 9):
    memoria_cache[0][i] = ram[int(bintodec(cache[0] + cache[1] + dectobin(i-1, 3)))]
    print(memoria_cache)

The code is supposed to only iterate through the first row, but in the output it changes both rows at once instead. Could someone shed a light on this matter?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    You probably used `memoria_cache = [[0]*9]*2`. That creates ONE 9-entry list, and makes two references to that same list. Changing either reference changes the list object, and is seen in both places. – Tim Roberts Aug 26 '21 at 20:03
  • [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/q/240178) possibly. – 001 Aug 26 '21 at 20:03
  • Please provide a [mre] (MRE) that includes initializing the matrix. – martineau Aug 26 '21 at 21:01

0 Answers0