Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My for loop isn't removing items in my array based on condition? Python [duplicate]

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???

like image 902
Alec Mather Avatar asked Nov 11 '18 18:11

Alec Mather


2 Answers

You can iterate through a copy of a list. This can be done by adding [:] in your for loop list moves[:].

Input

moves = [[3,-1],[4,-1],[5,-11], [2,-2]]
for move in moves[:]:
    if (move[0] < 0) or (move[1] < 0):
        moves.remove(move)

print(moves)

Output

[]
like image 143
Naveen Avatar answered Oct 13 '22 13:10

Naveen


Dont iterate and modify at the same time.

You can use a list comp or filter() to get a list that fits your needs:

moves = [[3,1],[4,-1],[5,1],[3,-1],[4,1],[5,-1],[3,1],[-4,1],[-5,1]]

# keep all values of which all inner values are > 0
f = [x for x in moves if all(e>0 for e in x)]

# same with filter()
k = list(filter(lambda x:all(e>0 for e in x), moves))

# as normal loop
keep = []
for n in moves:
    if n[0]>0 and n[1]>0:
        keep.append(n)

print(keep)

print(f) # f == k == keep  

Output:

[[3, 1], [5, 1], [4, 1], [3, 1]]

Doku for filter() and all() can be found at the overview on built in functions

like image 32
Patrick Artner Avatar answered Oct 13 '22 12:10

Patrick Artner