python adding list inside list unexpected behavior -


python 2.7.3 - debian 7 - 32 bits

i trying add lists(listado) inside list(tabla), when tabla printed elements in tabla same , besides last list added !!!!

tabla = [] listado = [0,0,0]     lista_base = range(100)                         elemento in lista_base:     listado[0] = elemento     listado[1] = elemento+1     listado[2] = elemento+2     tabla.append(listado)       # <--- wrong here ??     print(listado)              # <--- works fine. print each *listado*.  print(tabla)                     

you changing content of same list, , adding reference in tabla. so, lists in tabla same last list added.

you should create new list each time in loop. try changing loop to:

for elemento in lista_base:     listado = [elemento, elemento+1, elemento+2]     tabla.append(listado)      

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -