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

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

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


    dcrespo, Oct 27, 2005
    1. Advertisements

  2. dcrespo

    skip Guest

    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
    1. Advertisements

  3. dcrespo

    Steve Holden Guest

    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 ...

    Steve Holden, Oct 27, 2005
  4. dcrespo

    Mike Meyer Guest

    Iterate over everything in the dictionary:

    for key, item in mydict.items():
    if item is None:
    mydict[key] = 'Null'

    Mike Meyer, Oct 27, 2005
  5. 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 ;-)

    Bengt Richter
    Bengt Richter, Oct 28, 2005
  6. ....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_

    Alex Martelli, Oct 28, 2005
  7. 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 at modulix, Oct 28, 2005
  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

    I've been looking for OpenSSL for python. I found pyOpenSSL, but it
    requires the OpenSSL library, which I only found on, 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.

    dcrespo, Oct 28, 2005
  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

    dcrespo, Oct 28, 2005
  10. 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'
    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 D'Aprano, Oct 29, 2005
  11. dcrespo a écrit :
    Let us know if you find one...
    Being wise, I don't use Windows !-)
    Bruno Desthuilliers, Oct 30, 2005
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.