tharvey - 1 month ago 11

Python Question

I am trying to remove rows in an array that have

`0`

`for i in range (5680):`

if games[i][0] == 0 and games[i][1] == 0:

games.pop(i)

print(games)

Putting a print statement of

`games[i][0]`

`games[i][1]`

`0`

`list out of range`

`5680`

`5`

No matter what I try changing the error is still thrown. Whats going on?

Answer

Trying to change an object that you are iterating through is almost always a bad idea. In your case after first `pop()`

you have list that is already smaller than 5680. So when you get to 5679 you are already out of range.

Try this:

```
games = [g for g in games if g[0] != 0 or g[1] != 0]
```

or

```
games = list(filter(lambda g: g[0] != 0 or g[1] !=0, games))
```