Why does a for-loop with pop-method (or del statement) not iterate over all list elements

前端 未结 5 1823
长情又很酷
长情又很酷 2020-12-11 03:46

I am new to Python and experimenting with lists I am using Python 3.2.3 (default, Oct 19 2012, 20:13:42), [GCC 4.6.3] on linux2

Here is my samplecode



        
5条回答
  •  抹茶落季
    2020-12-11 04:35

    Code

    l = [1,2,3,4,5,6]
    for i in range(len(l)):
        l.pop(0)
        print(l)
    

    returns

    [2, 3, 4, 5, 6]
    [3, 4, 5, 6]
    [4, 5, 6]
    [5, 6]
    [6]
    []
    

提交回复
热议问题