Why does del list[0] in a for-loop only delete half the list?

自古美人都是妖i 提交于 2021-02-08 06:34:02

问题


I have the following code and it only deletes half the list using a for loop. Why does this happen? Also, how should I delete the first element of the list WHILE traversing through it?

list = [1,2,3,4,5,6,7,8,9,10]
for x in list:
    print list[0]
    del list[0]
print list 

Output:

1 
2
3
4
5
[6, 7, 8, 9, 10]

回答1:


The problem is that you delete from the list but the list-iterator doesn't know that and happily processes the "remaining" list.

So in the first iteration the iterator is at index 0 and you remove index 0, in the next iteration the iterator returns index 1 (which would be the item at index 2 before you removed the first item) and it removes index 0. In the next iteration you get the item at index 2 (which was at index 4 originally but since you removed two items is now at index 2) and so on. Finally it will stop as soon as the index is greater than the items remaining in the list, given that you remove one item for each item processed that's in the middle (half) of the original list.

Long story short: Don't modify the list you're iterating over.


If you really want to do something like that then you could use a while loop:

lst = [1,2,3,4,5,6,7,8,9,10]
while lst:   # as long as the lst contains items
    print lst[0]
    del lst[0]
print lst 

or iterate over a copy:

lst = [1,2,3,4,5,6,7,8,9,10]
for x in lst[:]:   # [:] makes a shallow copy of a list
    print lst[0]
    del lst[0]
print lst 

Note: list is the name of a built-in function of Python, so you would shadow this function if you have a variable with the same name. That's why I changed the variable name to lst.




回答2:


What is happening here is that you are changing the list as you are iterating over it, lets look at the iterations of the loop

1st iteration:

pointer
  |
  V
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

now you delete the number 1 from the list, but THE POINTER WILL STILL MOVE TO THE NEXT ITEM, that is the problem

2nd iteration:

  pointer
     |
     V
 [2, 3, 4, 5, 6, 7, 8, 9, 10]

Next iteration the number 2 will be deleted and the pointer will advance.

3rd iteration:

     pointer
        |
        V
 [3, 4, 5, 6, 7, 8, 9, 10]



4th iteration:

        pointer
           |
           V
 [4, 5, 6, 7, 8, 9, 10]



5th iteration (last one):

           pointer
              |
              V
 [5, 6, 7, 8, 9, 10]

Now you print the list and get [6, 7, 8, 9, 10]. You can see that what I said is in fact really what is going on by changing the line print list[0] to print list[0], x, that way you can see the pointer.

This will be the output:

1 1
2 3
3 5
4 7
5 9
[6, 7, 8, 9, 10]

What can be done to fix this problem? any one of the following:

This will make x a number (an index of an item in the list) which means the loop will have len(list) iterations (that would be 10 iterations)

list = [1,2,3,4,5,6,7,8,9,10]

for x in range(len(list)):
    print list[0]
    del list[0]
print list 

This will make it so the loop iterates over a copy of the original list, therefore it will loop 10 times, which is enough to delete all the items in the list.

list = [1,2,3,4,5,6,7,8,9,10]
copy = [1,2,3,4,5,6,7,8,9,10]

for x in copy:
    print list[0]
    del list[0]
print list 


来源:https://stackoverflow.com/questions/45683615/why-does-del-list0-in-a-for-loop-only-delete-half-the-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!