By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,766 Members | 1,327 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,766 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Oct 27 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a

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
Oct 27 '05 #2

P: n/a
dcrespo wrote:
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Oct 27 '05 #3

P: n/a
"dcrespo" <dc*****@gmail.com> 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'

<mike

--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 27 '05 #4

P: n/a
On Thu, 27 Oct 2005 16:46:32 -0400, Mike Meyer <mw*@mired.org> wrote:
"dcrespo" <dc*****@gmail.com> 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
Oct 28 '05 #5

P: n/a
Bengt Richter <bo**@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
Oct 28 '05 #6

P: n/a
dcrespo wrote:
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'
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Oct 28 '05 #7

P: n/a
> 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

Oct 28 '05 #8

P: n/a
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

Oct 28 '05 #9

P: n/a
On Fri, 28 Oct 2005 05:23:00 -0700, dcrespo wrote:
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.


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[i]
if D[key] == None:
D[key] = 'Null'
else:
del L[i]
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! ;-)

--
Steven

Oct 28 '05 #10

P: n/a
dcrespo a écrit :
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.


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>
Oct 29 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.