Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to replace all None values with the string "Null" in a dictionary

Reply
Thread Tools

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

 
 
dcrespo
Guest
Posts: n/a
 
      10-27-2005
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

 
Reply With Quote
 
 
 
 
skip@pobox.com
Guest
Posts: n/a
 
      10-27-2005

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
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      10-27-2005
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/

 
Reply With Quote
 
Mike Meyer
Guest
Posts: n/a
 
      10-27-2005
"dcrespo" <(E-Mail Removed)> 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 <(E-Mail Removed)> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      10-28-2005
On Thu, 27 Oct 2005 16:46:32 -0400, Mike Meyer <(E-Mail Removed)> wrote:

>"dcrespo" <(E-Mail Removed)> 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
 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      10-28-2005
Bengt Richter <(E-Mail Removed)> 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
 
Reply With Quote
 
bruno at modulix
Guest
Posts: n/a
 
      10-28-2005
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 '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
dcrespo
Guest
Posts: n/a
 
      10-28-2005
> 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

 
Reply With Quote
 
dcrespo
Guest
Posts: n/a
 
      10-28-2005
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

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-28-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
DataGrid - BorderStyle="None" and BorderWidth="None" doesn't work for Firefox David Freeman ASP .Net 8 02-16-2011 11:03 AM
<var> is None vs. <var> == None Gerald Britton Python 7 01-24-2009 09:02 AM
testing for valid reference: obj vs. None!=obs vs. obj is not None alf Python 9 12-09-2006 05:00 AM
Is there a built-in method for transforming (1,None,"Hello!") to 1,None,"Hello!"? Daniel Crespo Python 5 11-13-2005 12:52 PM



Advertisments