I'm merging dictionaries together in series. Each one may/may not have lists as values. I'd like to merge indefinitely and, have the final merged dictionary contain only flat lists as values.
Any advice for how to write this as a single/simpler function?
def merge_dlist(dict_1, dict_2):
"""Merge_dlist accepts dictionaries to be merged."""
merged_dict = defaultdict(list)
merged_dict = merge_type(dict_1, merged_dict)
merged_dict = merge_type(dict_2, merged_dict)
return merged_dict
def merge_type(dictionary, merged_dict):
"""Merge dictionary keys. Append value items as strings to value list."""
for key, value in dictionary.items():
if isinstance(value, list):
for item in value:
merged_dict[key].append(item)
if isinstance(value, str):
merged_dict[key].append(value)
return merged_dict