python for loop to delete the list of elements, remove the dirty problem solving

1. a list a = [1,2,3,4,5] cycle remove elements:

a=[1,2,3,4,5]
for i in a:
    a.pop(0)
print(a)

Finally, a running found not [], the problem is mainly due to the deletion of elements, the entire list of elements will move forward, but it is the first i have been identified, is constantly increasing, and therefore it can not get the desired result.

Solution:

a=[1,2,3,4,5]
for i in a[:]:
    a.pop(0)
print(a)

The run finished a list becomes empty

Guess you like

Origin www.cnblogs.com/dushangguzhousuoli/p/11308274.html