0

I have the following strange behaveiour in my program and I would be very thankful if somebody can please help me.

data = [1, 4, 5, 6, 10, 15, 16, 17, 18, 22, 25, 26, 27, 28]

datacopy = copy.deepcopy(data)

for i in datacopy:
    datacopy.remove(i)
    

I would suspect datacopy to be an empty list after this, but strangely datacopy=[4, 6, 15, 17, 22, 26, 28]. The problem seems to be that i does not get every element of datacopy, which is something I do not understand.

Thanks for your support!

samabu
  • 181
  • 6
  • 4
    This is nothing to do with the copying. In fact there's no point in the *deep* copy because everything in the list is immutable. Don't change the length of a list while iterating over it! – jonrsharpe Dec 22 '20 at 09:07
  • Ah ok, so the problem is that I am changing the length of the list while iterating over it. It works by iterating over i in data. Thank you very much for that quick answer. – samabu Dec 22 '20 at 09:12

0 Answers0