generated from readthedocs/tutorial-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge_dicts.py
37 lines (32 loc) · 1.22 KB
/
merge_dicts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""
merge_dicts - Given dictionaries are merge into a new dict.
"""
__version__ = "0.2.0"
def merge_dicts(*dict_args: [{}]) -> [{}, bool]:
"""
merge_dicts: Given dictionaries are merge into a new dict.
:param dict_args: Optional "dict_args" as list of dict.
:type dict_args: dict or list(dict) or None
:raise AttributeError: If the dict_args is invalid.
:return: merged dict and list of merge conflicts.
:rtype: list(dict, list)
"""
result = {}
merge_conflict = False
merge_conflict_lists = []
for dictionary in dict_args:
current_list_of_merge_conflicts = []
for key, value in dictionary.items():
# Evaluate if two input dict have the same key, but different value.
# If so, add key to current_list_of_merge_conflicts.
if key in result:
if value == result[key]:
pass
else:
current_list_of_merge_conflicts.append(key)
merge_conflict = True
else:
# Add key and value to the dict.
result[key] = value
merge_conflict_lists.append(current_list_of_merge_conflicts)
return [result, merge_conflict]