How to replace all None values with the string "Null" in a dictionary

Alex Martelli aleaxit at yahoo.com
Fri Oct 28 06:34:25 CEST 2005


Bengt Richter <bokr at oz.net> wrote:
   ...
> Which is probably more efficient than one-liner updating the dict with
> 
>     mydict.update((k,'Null') for k,v in mydict.items() if v is None)

...which in turn is probably better than

_auxd = {None: "Null"}
newd = dict((k, _auxd.get(k, c) for k, c in mydict.items())

[which might be a nice idea if you wanted to do _several_
substitutions;-)...]


Alex



More information about the Python-list mailing list