0

Python 2.7.3 - Debian 7 - 32 bits

I am trying to add lists(listado) inside a list(tabla), but when tabla is printed all the elements in tabla are the same and besides it is the last list added !!!!

tabla = []
listado = [0,0,0]    
lista_base = range(100)                       

for elemento in lista_base:
    listado[0] = elemento
    listado[1] = elemento+1
    listado[2] = elemento+2
    tabla.append(listado)       # <--- What is wrong here ??
    print(listado)              # <--- This works fine. It is print each *listado*.

print(tabla)                    

2 Answers 2

1

You are changing the content of same list, and adding a reference to it in your tabla. So, all the lists in the tabla will be same as the last list added.

You should create a new list each time in the loop. Try changing your loop to:

for elemento in lista_base:
    listado = [elemento, elemento+1, elemento+2]
    tabla.append(listado)     
Sign up to request clarification or add additional context in comments.

Comments

1

You are manipulating the same list over and over again, not a copy.

Create a new list in the loop instead:

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

or create a copy of the list:

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

where [:] returns a full slice of all the elements. You could also use list(listado) or import the copy module and use copy.copy(listado) to create a copy of the existing list.

Appending a list to another list only adds a reference, and your code thus created many references to the same list that you kept changing in the loop.

You could have seen what was happening had you printed tabla on every loop. Printing listado on every loop iteration only shows that the state of that list was correct for that iteration, not that all the references to that list in tabla were changing along with it:

>>> tabla = []
>>> listado = [0, 0, 0]
>>> for elemento in range(3):
...     listado[0] = elemento
...     listado[1] = elemento+1
...     listado[2] = elemento+2
...     tabla.append(listado)
...     print 'tabla after iteration {}: {!r}'.format(elemento, tabla)
... 
tabla after iteration 0: [[0, 1, 2]]
tabla after iteration 1: [[1, 2, 3], [1, 2, 3]]
tabla after iteration 2: [[2, 3, 4], [2, 3, 4], [2, 3, 4]]

Notice how all tabla lists change together; they are in fact all the same list. If you create a new list instead, things work as expected:

>>> tabla = []
>>> for elemento in range(3):
...     listado = [elemento, elemento + 1, elemento + 2]
...     tabla.append(listado)
...     print 'tabla after iteration {}: {!r}'.format(elemento, tabla)
... 
tabla after iteration 0: [[0, 1, 2]]
tabla after iteration 1: [[0, 1, 2], [1, 2, 3]]
tabla after iteration 2: [[0, 1, 2], [1, 2, 3], [2, 3, 4]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.