Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are modifying the contents of the object <code>forms</code> that you are iterating over, when you say:</p> <pre><code>forms.remove((form,question)) </code></pre> <p>According to the <a href="http://docs.python.org/tutorial/controlflow.html" rel="noreferrer">Python documentation of the <code>for</code> statement</a>, this is not safe (the emphasis is mine):</p> <blockquote> <p>The for statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.</p> <p><b>It is not safe to modify the sequence being iterated over in the loop </b>(this can only happen for mutable sequence types, such as lists). If you need to modify the list you are iterating over (for example, to duplicate selected items) you must iterate over a copy. The slice notation makes this particularly convenient:</p> <pre><code>for x in a[:]: # make a slice copy of the entire list ... if len(x) &gt; 6: a.insert(0, x) </code></pre> </blockquote> <p>See also this paragraph from the <a href="http://docs.python.org/reference/compound_stmts.html#the-for-statement" rel="noreferrer">Python Language Reference</a> which explains exactly what is going on:</p> <blockquote> <p>There is a subtlety when the sequence is being modified by the loop (this can only occur for mutable sequences, i.e. lists). An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. <b>This means that if the suite deletes the current (or a previous) item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated).</b> Likewise, if the suite inserts an item in the sequence before the current item, the current item will be treated again the next time through the loop. </p> </blockquote> <p>There are a lot of solutions. You can follow their advice and create a copy. Another possibility is to create a new list as a result of your second <code>for</code> loop, instead of modifying <code>forms</code> directly. The choice is up to you...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload