[Python-Dev] a feature i'd like to see in python #1: better iteration control

"Martin v. Löwis" martin at v.loewis.de
Mon Dec 4 19:15:35 CET 2006


Brian Harring schrieb:
> For dict; it actually *cannot* work.  You can't remove keys from a 
> dict as you're iterating over it (can change the val of a key, but not 
> remove the key).

I think this is incorrect. The implementation could well support it,
putting a dummy object into the deleted key (which deletion needs
to do, anyway).

> So iter.delete would require fair bit of changes 
> internally to dict, either tracking what it's yielded already, or 
> forcing iterkeys to actually be iter(keys()) (creating an intermediate 
> list), which is worse for memory usage and general performance.

I don't think either is necessary; deletion could occur "directly".

> Set's suffer the same thing; can't change what it contains while 
> iterating, have to restart the iteration after a removal/addition.

Again, I think that's incorrect.

> Tuples are immutable, so end of discusion there.

True.

> Now... occasionally, have to do it admittedly.  But it's not something 
> you actaully want to be doing in your code all that much- admittedly 
> generating a new list to avoid that hit also sucks somewhat, but the 
> worst case there is far more behaved, a temp trade of space vs 
> runtime.

Also true.

Regards,
Martin


More information about the Python-Dev mailing list