I am using this code to merge one dict into another - if a key exists, it should merge the values from both dicts:
source_dict = defaultdict(set)
target_dict = defaultdict(set)
def merge_dict(source_dict, target_dict):
for source_key in source_dict:
if source_key in target_dict:
target_dict[source_key].update(source_dict[source_key])
else:
target_dict[source_key] = source_dict[source_key]
Is there a way to optimize the merge_dict function above? For example, to make it more concise or more efficient?