"dictionary changed size during iteration" error in Python 3 but not in Python 2

Chris Green cl at isbd.net
Sun Aug 23 10:43:13 EDT 2020


Cameron Simpson <cs at cskk.id.au> wrote:
> On 23Aug2020 10:00, Chris Green <cl at isbd.net> wrote:
> >I have a (fairly) simple little program that removes old mail messages
> >from my junk folder.  I have just tried to upgrade it from Python 2 to
> >Python 3 and now, when it finds any message[s] to delete it produces
> >the error:-
> >
> >    RuntimeError: dictionary changed size during iteration
> >
> >I can sort of see why I'm getting the error but have two questions:
> >
> >1 - Why doesn't it error in Python 2?
> 
> The dict internal implementation has changed. I don't know the 
> specifics, but it is now faster and maybe smaller and also now preserves 
> insert order.
> 
Ah, that probably explains it then.


> >2 - How do I fix it?
> 
> That standard way is to take a copy of what you're iterating over, 
> usually just the keys (consumes less memory).
> 
> BTW, this approach is standard for _any_ data structure you're iterating 
> over and modifying.
> 
> So:
> 
>     for k, v in d.iteritems():
>        ... d.remove(k) ...
> 
> might become:
> 
>     for k, v in list(d.iteritems()):
>        ... d.remove(k) ...
> 
I've been [not]understanding it the wrong way round!  I was somehow
trying to copy the iterator and then doing things to the copy, I now
see that you make a copy to iterate over (that isn't modified) and you
do what's necessary to the original.  Obvious really! :-)

-- 
Chris Green
·


More information about the Python-list mailing list