Well, first things first:
the "lists" go between []
and the tuples go between ()
... a tuple is immutable, a list can be modified instead.
therefore the following is NOT immutable (can be modified):
lista = [0, 0, 0, 0, 0]
To be immutable it should be replaced (for example):
secuencia = (0, 0, 0, 0, 0)
In this context, if it makes sense to talk about building lista_temp
. otherwise we would simply modify lista
. Apart from this clarification the following procedure is analogous
Python and many languages (which incorporate certain features of the functional paradigm) incorporate "slicing" that is, you can cut strings with indices, in turn copy their content into other elements, referencing them correctly:
>>>lista = [0, 0, 0, 0, 0]
>>>lista_temp = lista[:3] + list("c") + lista[4:]
>>> lista_temp
[0, 0, 0, 'c', 0]
It is not necessary to make any loop for
, this would be with respect to the first case.
On the other hand in the example that is shown later there is a problem regarding the dimensionality (or nesting) of the lists:
lista=[[[0,0,0],[0,0,0]],[[0,0,0],[0,0,0]]]
has two elements (this would look if len(lista)
is executed). therefore in lista_temp=[[[0,0,0],[0,0,0]],[["c",0,0],[0,0,0]]]
the second element of the list was modified (better said the first element of the second element of the main list), which is NOT the same as that shown in the first example where the fourth element is modified. (Keep in mind that the indexes start from zero therefore when the conditional checks that a=3
, is checking that it is in the fourth element)
Anyway, here I put what would be done using slicing (so that it is as in the second example):
>>> lista=[[[0,0,0],[0,0,0]],[[0,0,0],[0,0,0]]]
>>> lista_temp = lista[:1] + [ ["c",0,0] + lista[1][1:]]
>>> lista_temp
[[[0, 0, 0], [0, 0, 0]], ['c', 0, 0, [0, 0, 0]]]