title: Best method for changing a list while iterating over it
body: <p>I have several instances in a python script (v2.6) where I need to modify a list in-place. I need to pop values from the list in response to interactive input from the user and would like to know the cleanest method of doing this. Currently I have the very dirty solutions of a) setting items in the list that I want to remove to False and removing them with a filter or list comprehension or b) generating an entirely new list while going through the loop, which seems to be needlessly adding variables to the namespace and taking up memory.</p>
  
  <p>An example of this problem is as follows:</p>
  
  <pre><code>for i, folder in enumerate(to_run_folders):
      if get_size(folder) &lt; byte_threshold:
          ans = raw_input(('The folder {0}/ is less than {1}MB.' + \
                      ' Would you like to exclude it from' + \
                      ' compression? ').format(folder, megabyte_threshold))
          if 'y' in ans.strip().lower():
              to_run_folders.pop(i)
  </code></pre>
  
  <p>I would like to look at each folder in the list. If the current folder is less than a certain size, I want to ask the user if they want to exclude it. If they do, pop the folder from the list.</p>
  
  <p>The problem with this routine is that if I iterate over the list, I get unexpected behavior and early termination. If I iterate over a copy by slicing, pop doesn't pull the right value because the indices are shifted and the problem compounds as more items are popped. I have a need for dynamic list adjustment of this kind in other areas of my script as well. Is there any clean method for this kind of functionality?</p>