how to scrutch a dict()

Joost Molenaar j.j.molenaar at gmail.com
Thu Oct 21 08:44:22 EDT 2010


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 }

Joost

On 21 October 2010 06:32, Phlip <phlip2005 at gmail.com> wrote:
>
> Not Hyp:
>
> def _scrunch(**dict):
>    result = {}
>
>    for key, value in dict.items():
>        if value is not None:  result[key] = value
>
>    return result
>
> That says "throw away every item in a dict if the Value is None".
>
> Are there any tighter or smarmier ways to do that? Python does so
> often manage maps better than that...
>
> --
>  Phlip
>  http://zeekland.zeroplayer.com/
> --
> http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list