When you loop over a list with for i in my_list
, i
isn't bound to a list cell on each iteration. It's bound to the object the list cell referred to. Assigning a new value to i
binds i
to a new object, but there's no link between i
and the list that would cause the list cell to be affected.
An equivalent way to loop over a list would be
for index in range(len(my_list)):
i = my_list[index]
whatever_loop_body()
Hopefully, it's clearer in this formulation that i
is not an alias for the list cell. (Don't actually loop over lists like this.)