Could looping over a list using the for loop and using the pop and append method inside cause any issues? Is there an alternate safe way? I know in Java it causes problems, and so you use iterators either explicitly or implicitly. Is there something similar in Python?
My Thoughts:
It does cause issues, I tried this program:
l=[1,2]
for elem in l:
l.pop(0)
l.append(1)
print(elem)
The output was 1,1,2,1. Now I am removing an element and appending an element. So after the first iteration, l will become [2]->[2,1] (pop and append), then it becomes [1]->[1,1] not sure how it produces the current output and stops. It does cause some issues. Is there any logic behind this?