0

I wanted to write a simple script for create a matrix 10x10 with all the numbers from 1 to 99 appending to a list group of 10 element at each time. The result i expected was list[[1,2,3,4,5,6,7,8,9],[10,11,12,13,14,15,16,17,18,19],...] But the output is very strange:

here's the script:

lista=[]
lista2=[]
z=0
for a in range (10):
    lista2.clear()
    for x in range (10):
        lista2.append(z)
        z+=1
    print(lista2)
    lista.append(lista2)
    print(lista)

here's the output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[[10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]]
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
[[20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]]
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
[[30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39]]
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
[[40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49]]
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
[[50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]]
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69]
[[60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69]]
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79]
[[70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79]]
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89]
[[80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89]]
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
[[90, 91, 92, 93, 94, 95, 96, 97, 98, 99], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]]

I added print(lista2) to check that there were only 10 elements each time in it.

AMC
  • 2,642
  • 7
  • 13
  • 35
  • 2
    You're using the same `lista2` every time through the loop. Appending it to `lista` doesn't make a copy. – Barmar Mar 18 '20 at 21:33
  • 2
    Change `lista2.clear()` to `lista2 = []` – Barmar Mar 18 '20 at 21:33
  • Does this answer your question? [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – Brian61354270 Mar 18 '20 at 21:34
  • And instead of tracking `z` manually you could do `lista2.append(10 * a + x)`. – Matthias Mar 18 '20 at 21:35

1 Answers1

0
lista = []
z = 0

for i in range(10):
    lista2 = []
    for j in range(10):
        lista2.append(z)
        z += 1
    lista.append(lista2)

print(lista)

or just:

lista = [[10*i+j for j in range(10)] for i in range(10)]
print(lista)
iagerogiannis
  • 337
  • 3
  • 16
  • Your question is "Why function clear() does not act the same?". It could work if you replaced line "lista.append(lista2)" with "lista.append(lista2.copy())". In the first case when you append lista2 to lista, you just create another reference of lista2. This means that lista[i] and lista2 are the exact same list, located in a specific memory location. When you call clear() you empty your list's elements, so both lista[i] and lista2 are empty and then you just refresh the values. This goes on for every iteration. – iagerogiannis Mar 18 '20 at 23:25
  • In the second case when you use line "lista2 = []", you replace the variable with anohter empty list. lista[i] continues to exist but has different memory location and therefore values than lista2. When you use "lista2.copy()" you create a new copy of your list at a different memory location and now you are able to clear your initial lista2, without affecting lista[i]. – iagerogiannis Mar 18 '20 at 23:26
  • For further investigation try the following links: https://stackoverflow.com/questions/55897375/difference-between-list-vs-list-clear https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list – iagerogiannis Mar 18 '20 at 23:41