0

I am building a for loop but i face a strange behavior, like i need to delete items from list while loop but later get the full list again. but when i delete the item, it deletes from ancestor also. simple example for the issue.

a = [1,2,3,4,5]
b = a
b.remove(3)
print(a,b)

output

[1, 2, 4, 5] [1, 2, 4, 5]

But what i expected output as:

[1, 2, 3, 4, 5] [1, 2, 4, 5]

i couldn't figure out how to correctly reference so that ancestor will not be effected. Thanks a lot

GurhanCagin
  • 185
  • 2
  • 13

0 Answers0