how to scrutch a dict()

Neil Cerutti neilc at norwich.edu
Thu Oct 21 08:46:41 EDT 2010


On 2010-10-21, James Mills <prologic at shortcircuit.net.au> wrote:
> Rather than creating a new dict why don't you just do:
>
> def _scrunch(d):
>    for k, v in d.items():
>       if v is None:
>          del d[k]

In Python 3, where items returns an iterator, modifying the
dictionary in this way may lead to cirrhossis of the dictionary.
Here's another idea:

for k in [k for k, v in d.items() if v is None]:
  del d[k]

-- 
Neil Cerutti



More information about the Python-list mailing list