Mar-19-2019, 11:56 AM
on line 9 you don't mutate
Now, here is what you were looking/expecting:
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 sameNow, 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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs