how to scrutch a dict()

Hrvoje Niksic hniksic at xemacs.org
Thu Oct 21 16:21:47 CEST 2010


Joost Molenaar <j.j.molenaar at gmail.com> writes:

> Using a 2.7/3.x dictionary comprehension, since you don't seem to mind
> creating a new dictionary:
>
> def _scrunched(d):
>     return { key: value for (key, value) in d.items() if value is not None }

Note that a dict comprehension, while convenient, is not necessary for
this to be formulated as a single expression:

def _scrunched(d):
    return dict((key, value) for (key, value) in d.items() if value is not None)



More information about the Python-list mailing list