Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does the in operator in python behave when the list it is traversing changes
    primarykey
    data
    text
    <p>I encountered an interesting and puzzling situation while trying to remove all the empty strings from a list. I wrote the below code the first time.</p> <pre><code>lst=['###','','@@@','','$$$','','','%%%','','&amp;&amp;&amp;'] print "len:",len(lst) iteration=1 for item in lst: print iteration,":",lst,":",len(lst),":","'%s'"%item if item!='': pass else: lst.remove(item) iteration+=1 </code></pre> <p>It produces the following output:</p> <pre><code>len: 10 1 : ['###', '', '@@@', '', '$$$', '', '', '%%%', '', '&amp;&amp;&amp;'] : 10 : '###' 2 : ['###', '', '@@@', '', '$$$', '', '', '%%%', '', '&amp;&amp;&amp;'] : 10 : '' 3 : ['###', '@@@', '', '$$$', '', '', '%%%', '', '&amp;&amp;&amp;'] : 9 : '' 4 : ['###', '@@@', '$$$', '', '', '%%%', '', '&amp;&amp;&amp;'] : 8 : '' 5 : ['###', '@@@', '$$$', '', '%%%', '', '&amp;&amp;&amp;'] : 7 : '%%%' 6 : ['###', '@@@', '$$$', '', '%%%', '', '&amp;&amp;&amp;'] : 7 : '' </code></pre> <p>NOTE: The code doesn't work like it should. There are some empty strings in the output. I later found better ways like: list comprehensions: <code>[x for x in lst if x!='']</code> or creating a new list and copying the non empty strings to it, which happens to be more efficient than the above code because it doesn't involve shifting the position every time you remove an element from the list.</p> <p>I however have some questions regarding the output of the code above.</p> <p>First question is, why doesn't the loop run ten times(the iteration number is on the far left) because the original length of the list is ten. Second, if you look at the rightmost column, you realize that it doesn't print print the <code>@@@</code> string. It totally skips it!! My theory is that the <code>in</code> operator is sugar(most likely) for an index so that even if the length of the list changes the index keeps increasing by one. This would explain why on the third iteration the value of <code>i</code> is the empty string and not the <code>@@@</code> since <code>lst[2]</code> is <code>''</code>.</p> <p>Is there something I need to know when using the in operator?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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