Replace None in a python dictionary
Here's a recursive solution that also replaces None
s inside lists.
First we define a simple class, Null
, to act as the replacement for None
.
class Null(object):
def __repr__(self):
return 'Null'
NULL = Null()
def replace_none(data):
for k, v in data.items() if isinstance(data, dict) else enumerate(data):
if v is None:
data[k] = NULL
elif isinstance(v, (dict, list)):
replace_none(v)
# Test
data = {
1: 'one',
2: ['two', 2, None],
3: None,
4: {4: None, 44: 'four'},
5: {
5: [55, 56, None],
6: {66: None, 67: None},
8: [88, {9:'nine', 99:None}, 100]
}
}
print(data)
replace_none(data)
print(data)
output
{1: 'one', 2: ['two', 2, None], 3: None, 4: {44: 'four', 4: None}, 5: {8: [88, {9: 'nine', 99: None}, 100], 5: [55, 56, None], 6: {66: None, 67: None}}}
{1: 'one', 2: ['two', 2, Null], 3: Null, 4: {44: 'four', 4: Null}, 5: {8: [88, {9: 'nine', 99: Null}, 100], 5: [55, 56, Null], 6: {66: Null, 67: Null}}}
You can do it using object_pairs_hook
from json
module:
def dict_clean(items):
result = {}
for key, value in items:
if value is None:
value = 'default'
result[key] = value
return result
dict_str = json.dumps(my_dict)
my_dict = json.loads(dict_str, object_pairs_hook=dict_clean)
for k, v in my_dict.items():
if v is None:
my_dict[k] = "my default value"
# replace_none_with_empty_str_in_dict.py
raw = {'place': 'coffee shop', 'time': 'noon', 'day': None}
def replace_none_with_empty_str(some_dict):
return { k: ('' if v is None else v) for k, v in some_dict.items() }
print(replace_none_with_empty_str(raw))