title: Remove elements as you traverse a list in Python
body: <p>In Java I can do by using an <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Iterator.html" rel="noreferrer"><code>Iterator</code></a> and then using the <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Iterator.html" rel="noreferrer"><code>.remove()</code></a> method of the iterator to remove the last element returned by the iterator, like this:</p>
  
  <pre><code>import java.util.*;
  
  public class ConcurrentMod {
      public static void main(String[] args) {
          List&lt;String&gt; colors = new ArrayList&lt;String&gt;(Arrays.asList("red", "green", "blue", "purple"));
          for (Iterator&lt;String&gt; it = colors.iterator(); it.hasNext(); ) {
              String color = it.next();
              System.out.println(color);
              if (color.equals("green"))
                  it.remove();
          }
          System.out.println("At the end, colors = " + colors);
      }
  }
  
  /* Outputs:
  red
  green
  blue
  purple
  At the end, colors = [red, blue, purple]
  */
  </code></pre>
  
  <p>How would I do this in Python? I can't modify the list while I iterate over it in a for loop because it causes stuff to be skipped (see <a href="http://docs.python.org/reference/compound_stmts.html#index-759" rel="noreferrer">here</a>). And there doesn't seem to be an equivalent of the <code>Iterator</code> interface of Java.</p>