Python list.remove() skips next element in list

前端 未结 6 1731
夕颜
夕颜 2020-12-16 01:27

Python, but not programming, newbie here. I\'m programming with lists and have run into an interesting problem.

width = 2
height = 2

# Traverse the board
de         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 01:43

    The statement for square in squares just visits each item in the list in order: squares[0], then squares[1], then squares[2], and so on until it runs out of squares.

    Removing squares[0] shifts all the other items in the list to the left one slot; the original squares[1] is now squares[0], so the for loop will skip over it.

提交回复
热议问题