I have an array (moves) of arrays. I want to iterate through my moves array and set a condition for each element. The condition is, if either number in the element is negative, then I want to remove that element from the moves array. The loop does not remove my items correctly. BUT if I run it through the exact same loop twice, then it WILL remove the last element. This makes no sense to me. Using Python 3.6
moves = [[3,-1],[4,-1],[5,-1]]
for move in moves:
if move[0] < 0 or move[1] < 0:
moves.remove(move)
If you run this code, moves ends with a result of [[4,-1]] But if you run this result through the exact same for loop again, the result is []
I also tried doing this with many more elements and it's just not grabbing certain elements for some reason. Is this a bug with .remove()? This is what I tried...(In this I tried detecting nonnegative number to see if that was part of the issue, it wasn't)
moves = [[3,1],[4,1],[5,1],[3,1],[4,1],[5,1],[3,1],[4,1],[5,1]]
for move in moves:
if move[0] < 2 or move [1] < 2:
moves.remove(move)
The result of the above code is
moves = [[4, 1], [3, 1], [4, 1], [5, 1]]
Any ideas???