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

Discussion in 'Python' started by dcrespo, Oct 27, 2005.

  1. dcrespo

    dcrespo Guest

    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
    dcrespo, Oct 27, 2005
    #1
    1. Advertising

  2. dcrespo

    Guest

    Re: How to replace all None values with the string "Null" in adictionary

    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, 2005
    #2
    1. Advertising

  3. dcrespo

    Steve Holden Guest

    Re: How to replace all None values with the string "Null" in adictionary

    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/
    Steve Holden, Oct 27, 2005
    #3
  4. dcrespo

    Mike Meyer Guest

    Re: How to replace all None values with the string "Null" in adictionary

    "dcrespo" <> 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 <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Oct 27, 2005
    #4
  5. On Thu, 27 Oct 2005 16:46:32 -0400, Mike Meyer <> wrote:

    >"dcrespo" <> 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
    Bengt Richter, Oct 28, 2005
    #5
  6. Bengt Richter <> 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
    Alex Martelli, Oct 28, 2005
    #6
  7. 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 ''.split('@')])"
    bruno at modulix, Oct 28, 2005
    #7
  8. dcrespo

    dcrespo Guest

    > 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
    dcrespo, Oct 28, 2005
    #8
  9. dcrespo

    dcrespo Guest

    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
    dcrespo, Oct 28, 2005
    #9
  10. 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
    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! ;-)



    --
    Steven
    Steven D'Aprano, Oct 29, 2005
    #10
  11. 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>
    Bruno Desthuilliers, Oct 30, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotr Dobrogost
    Replies:
    6
    Views:
    98
    Steven D'Aprano
    Aug 30, 2013
  2. length power
    Replies:
    2
    Views:
    74
    Rustom Mody
    Apr 10, 2014
  3. Skip Montanaro
    Replies:
    0
    Views:
    59
    Skip Montanaro
    Apr 10, 2014
  4. Johannes Schneider

    Re: why i have the output of [None, None, None]

    Johannes Schneider, Apr 10, 2014, in forum: Python
    Replies:
    0
    Views:
    50
    Johannes Schneider
    Apr 10, 2014
  5. Terry Reedy
    Replies:
    0
    Views:
    61
    Terry Reedy
    Apr 10, 2014
Loading...

Share This Page