FAQ
My code fragment below:

for key1 in dict1.keys():
for key2 in dict1[key1]:
if dict1[key1][key2] == None:
del dict1[key1][key2]

gives the following error:
RuntimeError: dictionary changed size during iteration

Any suggestion on how to code this please.

Thanks
Colin Brown
PyNZ

Search Discussions

  • Skip Montanaro at Nov 3, 2003 at 9:20 pm
    Colin> for key1 in dict1.keys():
    Colin> for key2 in dict1[key1]:
    Colin> if dict1[key1][key2] == None:
    Colin> del dict1[key1][key2]

    Should be

    for key1 in dict1.keys():
    for key2 in dict1[key1].keys():
    if dict1[key1][key2] == None:
    del dict1[key1][key2]

    Note that the inner for loop explicitly calls .keys(). That creates an
    explicit list. You loop over that instead of an iterator. Why did you
    choose to call dict1.keys() but not dict1[key1].keys()?

    Skip
  • Emile van Sebille at Nov 3, 2003 at 9:35 pm

    Colin Brown:
    My code fragment below:

    for key1 in dict1.keys():
    here you realize an iterable list
    for key2 in dict1[key1]:
    here you step through an iterator. If you change this to
    for key2 in dict1[key1].keys():

    and assuming I've guessed appropriately, you might get what you're looking
    for.
    if dict1[key1][key2] == None:
    del dict1[key1][key2]
    Emile van Sebille
    emile at fenx.com
  • Colin Brown at Nov 3, 2003 at 9:35 pm
    OOPS!

    Coding slip I'm afraid, sorry. I was thinking that the
    code was failing because keys() were being evaluated
    dynamically. I then tried to think how to queue the
    deletes to be done after the loop but could not
    figure out how to do this.

    Colin

    "Colin Brown" <cbrown at metservice.com> wrote in message
    news:3fa6c327$1 at news.iconz.co.nz...
    My code fragment below:

    for key1 in dict1.keys():
    for key2 in dict1[key1]:
    if dict1[key1][key2] == None:
    del dict1[key1][key2]

    gives the following error:
    RuntimeError: dictionary changed size during iteration

    Any suggestion on how to code this please.

    Thanks
    Colin Brown
    PyNZ

  • Peter Otten at Nov 3, 2003 at 10:02 pm

    Colin Brown wrote:

    My code fragment below:

    for key1 in dict1.keys():
    for key2 in dict1[key1]:
    if dict1[key1][key2] == None:
    del dict1[key1][key2]

    gives the following error:
    RuntimeError: dictionary changed size during iteration

    Any suggestion on how to code this please.

    Thanks
    Colin Brown
    PyNZ
    In addition to Emile van Sebille's fix, the following tries to avoid
    dictionary lookups:

    # dict1 is not changed so we need not copy the values
    for innerdict in dict1.itervalues():
    # innerdict is changed so let's make a copy of the items
    for key, value in innerdict.items():
    if value is None:
    del innerdict[key]


    Peter

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedNov 3, '03 at 9:09p
activeNov 3, '03 at 10:02p
posts5
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase