how to scrutch a dict()

James Mills prologic at shortcircuit.net.au
Thu Oct 21 00:47:47 EDT 2010


On Thu, Oct 21, 2010 at 2:32 PM, 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...

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]

cheers
James

-- 
-- James Mills
--
-- "Problems are solved by method"



More information about the Python-list mailing list