title: How to remove all objects in a list from a for loop with python?
body: <p>I feel like this should be a pretty simple process but for some reason I cannot get it to work.</p>
  <p>Goal: Loop through a list of objects and if they match a condition remove it.</p>
  <p>Issue: This works until it needs to remove all objects from the list. It will not remove the last item.</p>
  <p>Example (clearly simplified by same results):</p>
  <pre><code>def some_function(val):
      return True
  on_shift2 = ['user1', 'user2', 'user3']
  print(&quot;MY ON SHIFT2&quot;, on_shift2)
  for idx, val in enumerate(on_shift2):
      print(f&quot;Index: {idx} &amp; Value: {val}&quot;)
      if len(on_shift2) == 1:
          del (on_shift2) # remove the whole list
      else:
          check_user = some_function(val) # function to check if user is in list and if so return true
          if check_user:
              on_shift2.remove(val)
              # Also tried the following
              # del on_shift[idx] # Same result
              # on_shift.pop(idx) # Same result
  print(&quot;MY ON SHIFT2&quot;, on_shift2)
  </code></pre>
  <p>Results:</p>
  <pre><code>MY ON SHIFT2 ['user1', 'user2', 'user3']
  Index: 0 &amp; Value: user1
  Index: 1 &amp; Value: user3
  MY ON SHIFT2 ['user2']
  </code></pre>
  <p>Now the really odd part here is no matter what is in the list, it always seems to keem the middle item (i.e. user2). If i changed those values to chicken, horse, dog - the remaining item would be horse. Not sure if that's important but thought i would bring it up.</p>
  <p>Also, no matter the which method i choose to remove the the items from the list (pop, remove or del), the 3rd (or last) item never gets removed if they all return true from &quot;some_function()&quot;.</p>
  <p>Also when looping through the if condition on the len(on_shift) never does equal 1, it always stops at 2. Never makes it to the 3rd (or last) object if the all return true from &quot;some_fuction()&quot;, so my if statement to delete the list never happens.</p>
  <p>If i print the val for the loop without removing anything from the list, all 3 names show up. I have to be doing something silly here.</p>
  <p>Thoughts?</p>