list1 = [1, 2, 3]
list2 = list1
for i, x in enumerate(list2):
print(list1, list2)
del list2[i]
returns
[1, 2, 3] [1, 2, 3]
[2, 3] [2, 3]
[2] [2]
when I expect it to return
[1, 2, 3] [1, 2, 3]
[1, 2, 3] [2, 3]
[1, 2, 3] [2]
I am new to python and was wondering if I am doing something wrong or missing something.
edit:
list1 = [1, 2, 3]
list2 = list1.copy()
for i in list1:
print(list1, list2)
list2.remove(i)
fixed it
>Solution :
If you want to solve this, you need to create a copy of x into a new item which can be done with the copy module
from copy import copy
list1 = [1, 2, 3]
list2 = copy(list1)
for i, x in enumerate(list2):
print(list1, list2)
del list2[i]