Strange behaviour with reversed()

Dustan DustanGroups at gmail.com
Thu Oct 18 18:06:26 EDT 2007


On Oct 18, 3:52 am, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Thu, 18 Oct 2007 15:24:27 +1000, Ben Finney wrote:
> > Steven D'Aprano <st... at REMOVE-THIS-cybersource.com.au> writes:
>
> >> and help(reversed) but neither gives any insight to what happens when
> >> you use reversed() on a sequence, then modify the sequence.
>
> > I would think the answer is the same for any question about modifying
> > sequences while iterating: "undefined, therefore don't do that".
>
> That's not correct. You can modify sequences while iterating, and the
> results are often perfectly predictable (but watch out for off-by-one
> errors):

Perhaps a better word to use here would have been "undocumented", and
therefore unsafe to use, as it might differ in different versions, or
in different data-types. Dictionaries cannot continue iteration once
they have changed size.




More information about the Python-list mailing list