First of all, I think you should read about Iteration in python.
Just to clear things up:
list1 = [[1, 2]]
means you have list1
containing a list, which contains the list [1,2]
at list1[0]
.
list2 = [[[1, 2]]]
means you have list2
containing the list [[1, 2]]
.
Secondly, in order to change the value of the first item in the list, from [1,2]
to [88,88]
, you can write:
list1 = [[1, 2]]
for item in list1:
item[0] = 88
item[1] = 88
print(list1)
output:
[[88, 88]]
Now, let's explain:
For each iteration of the for loop the variable item
is assigned with just a copy of the value of an item in the list, so changes made to item
won't be reflected in the list.
That is why in your first attempt, (with list1
) when you iterated over the list - item = [88, 88]
only changed the copy and not the actual list (the copy here was of the list [1,2]
).
In your second attempt, i.e:
for item in list2:
item[0] = [88, 88]
For each iteration of the for loop, you are accessing item[0]
which is a copy of the reference to the first element in item
. Therefore when you assign value to it, the value is changed in the list.