title: append/extend list in loop
body: <p>I would like to extend a list while looping over it:</p>
  
  <pre><code>for idx in xrange(len(a_list)):
      item = a_list[idx]
      a_list.extend(fun(item))
  </code></pre>
  
  <p>(fun is a function that returns a list.) </p>
  
  <p>Question:<br/>
  Is this already the best way to do it, or is something nicer and more compact possible?</p>
  
  <p>Remarks:</p>
  
  <pre><code>from matplotlib.cbook import flatten
  a_list.extend(flatten(fun(item) for item in a_list))
  </code></pre>
  
  <p>should work but I do not want my code to depend on matplotlib.</p>
  
  <pre><code>for item in a_list:
      a_list.extend(fun(item))
  </code></pre>
  
  <p>would be nice enough for my taste but seems to cause an infinite loop.</p>
  
  <p>Context:<br/>
  I have have a large number of nodes (in a dict) and some of them are special because they are on the boundary.
  'a_list' contains the keys of these special/boundary nodes. Sometimes nodes are added and then every new node that is on the boundary needs to be added to 'a_list'. The new boundary nodes can be determined by the old boundary nodes (expresses here by 'fun') and every boundary node can add several new nodes.</p>