I've known and relied on the behavior since Python 1.4. I mean, not often,
but it's been consistent.
In the case where I want to loop over what the loop was at start, rather
than what it might become, a slice comes in handy:
for item in mylist[:]:
# do stuff, maybe mutate list
On Sat, Oct 9, 2021, 9:37 PM Paul Bryan <[email protected]> wrote:
> Is this behavior of list iteration guaranteed by documentation anywhere? A
> quick search didn't yield any results for me.
>
> If not, I suggest either:
>
> a) it gets documented so developers can rely on it, or
> b) the lack of guarantee is documented so that we can reserve a change of
> behavior later.
>
> Paul
>
>
> On Sun, 2021-10-10 at 13:17 +1300, Greg Ewing wrote:
>
> On 9/10/21 11:24 am, Tyler Hou via Python-ideas wrote:
>
> Right now, the following code is valid in Python 3.9 (and infinitely
> loops):
>
> ```
> lst = [1, 2, 3]
> for i in lst:
> lst.append(i)
> ```
>
> 1. If the size of a list, set, or dict changes, invalidate all existing
> iterators to that containers.
>
>
> This would break Plex. I use a loop like that as part of the
> NFA-to-DFA conversion to calculate the epsilon-closure of a
> state (although obviously there are conditions on adding things
> to the list that ensure it terminates).
>
>
> _______________________________________________
> Python-ideas mailing list -- [email protected]
> To unsubscribe send an email to [email protected]
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/[email protected]/message/I76YHMMZESG55EBSQRJURXLFHKB6TQTF/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/QEDBND3GEE3ML3FTJXB43SBPMOJ5UCRE/
Code of Conduct: http://python.org/psf/codeofconduct/