I wanted to know if it is safe (documented behavior?) To remove the iterator domain space when executed in Python.
Consider the code:
import os import sys sampleSpace = [ x*x for x in range( 7 ) ] print sampleSpace for dx in sampleSpace: print str( dx ) if dx == 1: del sampleSpace[ 1 ] del sampleSpace[ 3 ] elif dx == 25: del sampleSpace[ -1 ] print sampleSpace
'sampleSpace' is what I call the "iterator domain space" (if there is a more suitable word / phrase, I know the lemma).
What I am doing is removing the values from it while the "dx" iterator is working through it.
Here is what I expect from the code:
Iteration versus element being pointed to (*): 0: [*0, 1, 4, 9, 16, 25, 36] 1: [0, *1, 4, 9, 16, 25, 36] ( delete 2nd and 5th element after this iteration ) 2: [0, 4, *9, 25, 36] 3: [0, 4, 9, *25, 36] ( delete -1th element after this iteration ) 4: [0, 4, 9, 25*] ( as the iterator points to nothing/end of list, the loop terminates )
.. and here is what I get:
[0, 1, 4, 9, 16, 25, 36] 0 1 9 25 [0, 4, 9, 25]
As you can see, what I expect is what I get, which contradicts the behavior that I had with other languages in this scenario.
Therefore - I wanted to ask you if there is any rule like “iterator becomes invalid if you mutate its space during iteration” in Python?
How safe (documented behavior?) In Python to do such things?