[EMAIL PROTECTED] wrote:
> Better:
>
> def clean(d):
>     for key,val in d.items():
>         if isinstance(val, dict):
>             val = clean(val)
>         if val is None or val == {}:
>             del d[key]
>     return d

Can you delete values from a dictionary whilst iterating over its items
?

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

>
> a = {1: {2: 2, 3: {1: None, 2: 2}}, 2: 2, 3: None}
> print clean(a) # Out: {1: {2: 2, 3: {2: 2}}, 2: 2}
> b = {1: {1: {1: None, 2: {1: None}}}, 2: 2, 3: None}
> print clean(b) # Out: {2: 2}
> c = {1: {1: {1: None, 2: {1: None}}}, 2: 2, 3: 0}
> print clean(c) # Out: {2: 2, 3: 0}

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to