On Thu, 27 Oct 2005 16:46:32 -0400, Mike Meyer <[EMAIL PROTECTED]> wrote:

>"dcrespo" <[EMAIL PROTECTED]> writes:
>
>> Hi all,
>>
>> How can I replace all None values with the string 'Null' in a
>> dictionary?
>
>Iterate over everything in the dictionary:
>
>for key, item in mydict.items():
>    if item is None:
>       mydict[key] = 'Null'
>
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)

as in

 >>> mydict = dict(a=1, b=None, c=3, d=None, e=5)
 >>> mydict
 {'a': 1, 'c': 3, 'b': None, 'e': 5, 'd': None}
 >>> mydict.update((k,'Null') for k,v in mydict.items() if v is None)
 >>> mydict
 {'a': 1, 'c': 3, 'b': 'Null', 'e': 5, 'd': 'Null'}
 
(too lazy to measure ;-)

Regards,
Bengt Richter
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to