on line 9 you don't mutate
start
, you create new list, i.e.
start
now points to different list:
start = [1,2,3,4,5]
print(f'id of start: {id(start)}')
new = []
print(f'id of new: {id(new)}')
t = True
for s in start:
new.append(start)
if t:
start.pop(0)
t = False
start = ['test']
print(f'id of start: {id(start)}')
print(new)
print(f'id of new: {id(new)}')
Output:
id of start: 4088456
id of new: 32132424
id of start: 32124040
[[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5]]
id of new: 32132424
>>>
as you can see the id of start changes, while the id of new is the same
Now, here is what you were looking/expecting:
start = [1,2,3,4,5]
print(f'id of start: {id(start)}')
new = []
print(f'id of new: {id(new)}')
t = True
for s in start:
new.append(start)
if t:
start.pop(0)
t = False
print(new)
start[0] = 'test'
print(f'id of start: {id(start)}')
print(new)
print(f'id of new: {id(new)}')
Output:
id of start: 4153992
id of new: 31997384
[[2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5], [2, 3, 4, 5]]
id of start: 4153992
[['test', 3, 4, 5], ['test', 3, 4, 5], ['test', 3, 4, 5], ['test', 3, 4, 5]]
id of new: 31997384
>>>
here you mutate the original
start
(
id
is not changed) and you can see that change is also reflected in
new