I am using python 2.7. I am trying to simulate popping of element from a list using the pop function. But I'm getting an inconsistent result.
Code using a variable list
list_pop = [1,2,3]
for x in list_pop:
print list_pop
y = list_pop.pop(0)
print y
Result:
[1, 2, 3]
1
[2, 3]
2
Code without using variable to hold the list
list_pop = [1,2,3]
for x in [1,2,3]:
print list_pop
y = list_pop.pop(0)
print y
[1, 2, 3]
1
[2, 3]
2
[3]
3
- I have tried popping index 0 from a list with one element it's working. I'm wondering why my first code halts printing after number 2 but when I print the list_pop it gives [3] the first code should still iterate through it and print 3. I'm expecting same results for both code. What am I missing here?