Why is list.remove only removing every second item?

后端 未结 6 1610
执念已碎
执念已碎 2021-01-23 04:59

In my Python 2.7.2 IDLE interpreter:

>>> mylist = [1, 2, 3, 4, 5]
>>> for item in mylist:
        mylist.remove(item)

>>> mylist
[2,          


        
6条回答
  •  悲&欢浪女
    2021-01-23 05:36

    You are modifying your list while you are looping through it, which is very bad practice.

提交回复
热议问题