Remove certain keys from a dictionary in python

Posted by Margaret on Stack Overflow See other posts from Stack Overflow or by Margaret
Published on 2010-05-10T04:50:18Z Indexed on 2010/05/10 4:58 UTC
Read the original article Hit count: 329

Filed under:

I'm trying to construct a dictionary that contains a series of sets:

{Field1:{Value1, Value2, Value3}, Field2{Value4}}

The trouble is, I then wish to delete any fields from the dictionary that only have one value in the set. I have been writing code like this:

for field in FieldSet:
if len(FieldSet[field]) == 1:
    del(FieldSet[field])

But receive the error "RuntimeError: dictionary changed size during execution". (Not surprising, since that's what I'm doing.) It's not the be-all and end-all if I have to knock together some sort of workaround, but is it possible to do this?

© Stack Overflow or respective owner

Related posts about python