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

D

dcrespo

Hi all,

How can I replace all None values with the string 'Null' in a
dictionary?

For example:
convert this:
a = {'item1': 45, 'item2': None}

into this:
a = {'item1': 45, 'item2': 'Null'}

Thanks

Daniel
 
S

skip

Daniel> How can I replace all None values with the string 'Null' in a
Daniel> dictionary?

a = {'item1': 45, 'item2': None}
for key in a:
if a[key] is None:
a[key] = "Null"

Skip
 
S

Steve Holden

dcrespo said:
Hi all,

How can I replace all None values with the string 'Null' in a
dictionary?

For example:
convert this:
a = {'item1': 45, 'item2': None}

into this:
a = {'item1': 45, 'item2': 'Null'}
for k in a:
if a[k] is None:
a[k] = 'Null'

You aren't doing this to create SQL statements, are you? If so,
parameterized queries are the way to go ...

regards
Steve
 
M

Mike Meyer

dcrespo said:
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'

<mike
 
B

Bengt Richter

dcrespo said:
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
{'a': 1, 'c': 3, 'b': 'Null', 'e': 5, 'd': 'Null'}

(too lazy to measure ;-)

Regards,
Bengt Richter
 
A

Alex Martelli

Bengt Richter said:
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
 
B

bruno at modulix

dcrespo said:
Hi all,

How can I replace all None values with the string 'Null' in a
dictionary?

For example:
convert this:
a = {'item1': 45, 'item2': None}

into this:
a = {'item1': 45, 'item2': 'Null'}

I think it would be time for you to read the Fine Manual...

for key in a:
if a[key] is None:
a[key] = 'Null'
 
D

dcrespo

I think it would be time for you to read the Fine Manual...

hi, thanks for your answer... I really did it the same way you
suggested, but I forgot to tell you that I wanted to get a better way
for doing it.

By the way, knowing your wisdom, what do I have to install to get the
following code work (Win XP, Python 2.4.2)

---------------------------------------------
from OpenSSL import SSL
import config

KEY_FILE = config.SSL_KEY_FILE
CERT_FILE = config.SSL_CERT_FILE

--------------------------------------------
I've been looking for OpenSSL for python. I found pyOpenSSL, but it
requires the OpenSSL library, which I only found on
http://www.openssl.org/, but don't know how to install.

Other thing is the "config" module... I'm lost. Someone knows? :-S

My main purpose is to enable XML-RPC SERVER over an SSL connection.

Thanks
 
D

dcrespo

Thanks... I did it right that way, but asked it without telling how I
did it just to see what are the occurences of others. I thing there's
no better/faster solution.

Many thanks

Daniel
 
S

Steven D'Aprano

hi, thanks for your answer... I really did it the same way you
suggested, but I forgot to tell you that I wanted to get a better way
for doing it.

What was wrong with the way you used?

Was it too easy to understand? Not difficult enough? Too few bugs?

You could try something like this:

def substitute(D):
L = D.keys()[:]
i = 0
while (i < len(L)) is True:
key = L
if D[key] == None:
D[key] = 'Null'
else:
del L
i = i + 1
return D


D = {'item1': None, 'item2': 23, 'item3': 42, 'item4': None, 'item5': 15}

print substitute(D)

gives {'item1': 'Null', 'item2': 23, 'item3': 42, 'item4': 'Null',
'item5': 15} as needed.

And I really, really hope this is of no hope whatsoever! ;-)
 
B

Bruno Desthuilliers

dcrespo a écrit :
hi, thanks for your answer... I really did it the same way you
suggested, but I forgot to tell you that I wanted to get a better way
for doing it.

Let us know if you find one...
By the way, knowing your wisdom,
what do I have to install to get the
following code work (Win XP, Python 2.4.2)

<troll>
Being wise, I don't use Windows !-)
</troll>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top