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

Multi tool use
Multi tool use











up vote
2
down vote

favorite













This question already has an answer here:




  • How to remove items from a list while iterating?

    23 answers



  • list comprehension vs. lambda + filter

    14 answers




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










share|improve this question















marked as duplicate by Patrick Artner, Thierry Lathuille, Billal Begueradj, lucascaro, Devon_C_Miller Nov 12 at 5:01


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    Do not iterate and modify the list you iterate at the same time.
    – Patrick Artner
    Nov 11 at 18:46










  • So then how do I filter the list based on a condition?
    – Alec Donald Mather
    Nov 11 at 18:48










  • In your 2nd example nothing would beleft as all elements have onne inner element < 2 .. what are you trying to do?
    – Patrick Artner
    Nov 11 at 18:51










  • Yoiur 1st example would also not have anything left - as all elements contain 1 eleemnt less then 0
    – Patrick Artner
    Nov 11 at 18:52










  • @PatrickArtner yes I'm aware that that is what is SUPPOSED to happen, but as you can see from the output, that isn't what's happening. That's what the question is about :)
    – Alec Donald Mather
    Nov 11 at 19:08















up vote
2
down vote

favorite













This question already has an answer here:




  • How to remove items from a list while iterating?

    23 answers



  • list comprehension vs. lambda + filter

    14 answers




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










share|improve this question















marked as duplicate by Patrick Artner, Thierry Lathuille, Billal Begueradj, lucascaro, Devon_C_Miller Nov 12 at 5:01


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    Do not iterate and modify the list you iterate at the same time.
    – Patrick Artner
    Nov 11 at 18:46










  • So then how do I filter the list based on a condition?
    – Alec Donald Mather
    Nov 11 at 18:48










  • In your 2nd example nothing would beleft as all elements have onne inner element < 2 .. what are you trying to do?
    – Patrick Artner
    Nov 11 at 18:51










  • Yoiur 1st example would also not have anything left - as all elements contain 1 eleemnt less then 0
    – Patrick Artner
    Nov 11 at 18:52










  • @PatrickArtner yes I'm aware that that is what is SUPPOSED to happen, but as you can see from the output, that isn't what's happening. That's what the question is about :)
    – Alec Donald Mather
    Nov 11 at 19:08













up vote
2
down vote

favorite









up vote
2
down vote

favorite












This question already has an answer here:




  • How to remove items from a list while iterating?

    23 answers



  • list comprehension vs. lambda + filter

    14 answers




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










share|improve this question
















This question already has an answer here:




  • How to remove items from a list while iterating?

    23 answers



  • list comprehension vs. lambda + filter

    14 answers




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





This question already has an answer here:




  • How to remove items from a list while iterating?

    23 answers



  • list comprehension vs. lambda + filter

    14 answers








python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 18:59









Patrick Artner

19.1k51940




19.1k51940










asked Nov 11 at 18:45









Alec Donald Mather

112




112




marked as duplicate by Patrick Artner, Thierry Lathuille, Billal Begueradj, lucascaro, Devon_C_Miller Nov 12 at 5:01


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Patrick Artner, Thierry Lathuille, Billal Begueradj, lucascaro, Devon_C_Miller Nov 12 at 5:01


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    Do not iterate and modify the list you iterate at the same time.
    – Patrick Artner
    Nov 11 at 18:46










  • So then how do I filter the list based on a condition?
    – Alec Donald Mather
    Nov 11 at 18:48










  • In your 2nd example nothing would beleft as all elements have onne inner element < 2 .. what are you trying to do?
    – Patrick Artner
    Nov 11 at 18:51










  • Yoiur 1st example would also not have anything left - as all elements contain 1 eleemnt less then 0
    – Patrick Artner
    Nov 11 at 18:52










  • @PatrickArtner yes I'm aware that that is what is SUPPOSED to happen, but as you can see from the output, that isn't what's happening. That's what the question is about :)
    – Alec Donald Mather
    Nov 11 at 19:08














  • 1




    Do not iterate and modify the list you iterate at the same time.
    – Patrick Artner
    Nov 11 at 18:46










  • So then how do I filter the list based on a condition?
    – Alec Donald Mather
    Nov 11 at 18:48










  • In your 2nd example nothing would beleft as all elements have onne inner element < 2 .. what are you trying to do?
    – Patrick Artner
    Nov 11 at 18:51










  • Yoiur 1st example would also not have anything left - as all elements contain 1 eleemnt less then 0
    – Patrick Artner
    Nov 11 at 18:52










  • @PatrickArtner yes I'm aware that that is what is SUPPOSED to happen, but as you can see from the output, that isn't what's happening. That's what the question is about :)
    – Alec Donald Mather
    Nov 11 at 19:08








1




1




Do not iterate and modify the list you iterate at the same time.
– Patrick Artner
Nov 11 at 18:46




Do not iterate and modify the list you iterate at the same time.
– Patrick Artner
Nov 11 at 18:46












So then how do I filter the list based on a condition?
– Alec Donald Mather
Nov 11 at 18:48




So then how do I filter the list based on a condition?
– Alec Donald Mather
Nov 11 at 18:48












In your 2nd example nothing would beleft as all elements have onne inner element < 2 .. what are you trying to do?
– Patrick Artner
Nov 11 at 18:51




In your 2nd example nothing would beleft as all elements have onne inner element < 2 .. what are you trying to do?
– Patrick Artner
Nov 11 at 18:51












Yoiur 1st example would also not have anything left - as all elements contain 1 eleemnt less then 0
– Patrick Artner
Nov 11 at 18:52




Yoiur 1st example would also not have anything left - as all elements contain 1 eleemnt less then 0
– Patrick Artner
Nov 11 at 18:52












@PatrickArtner yes I'm aware that that is what is SUPPOSED to happen, but as you can see from the output, that isn't what's happening. That's what the question is about :)
– Alec Donald Mather
Nov 11 at 19:08




@PatrickArtner yes I'm aware that that is what is SUPPOSED to happen, but as you can see from the output, that isn't what's happening. That's what the question is about :)
– Alec Donald Mather
Nov 11 at 19:08












2 Answers
2






active

oldest

votes

















up vote
3
down vote













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









share|improve this answer




























    up vote
    2
    down vote













    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






    share|improve this answer






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote













      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









      share|improve this answer

























        up vote
        3
        down vote













        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









        share|improve this answer























          up vote
          3
          down vote










          up vote
          3
          down vote









          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









          share|improve this answer












          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










          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 18:58









          Naveen

          647113




          647113
























              up vote
              2
              down vote













              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






              share|improve this answer



























                up vote
                2
                down vote













                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






                share|improve this answer

























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  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






                  share|improve this answer














                  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







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 11 at 19:11

























                  answered Nov 11 at 18:54









                  Patrick Artner

                  19.1k51940




                  19.1k51940















                      usl0NdCA2 jSW,rqc5VvZlXZV6Z0so S6mpqaqeBCHhqldM8GsEbNU,rBsGa0Z,N1FuZ1T,VsRrO1ldC,fum
                      ymz5VeDc eFwHM0wY,aLi9oVu UeIY2,3q i9W6XD0lnbvK,xhH5VE14jX

                      Popular posts from this blog

                      How to pass form data using jquery Ajax to insert data in database?

                      Guess what letter conforming each word

                      Run scheduled task as local user group (not BUILTIN)