I have two dictionaries items and u_items
items = {"A": 1, "B": 2, "C": 3}
u_items = {"D": 4, "B": 4, "E": 8, "C": 4}
I want to update the items dictionary with u_items so I did this
items.update((k + '_1' if k in items else k, v) for k, v in u_items.items())
such that I can differentiate keys from both dictionaries
Output:
items = {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'B_1': 4, 'E': 8, 'C_1': 4}
but when i update the items dictionary with another dictionary, let's say n_items, it replaces the value of B_1 instead of making it B_1_1
n_items = {"C":7, "B":9}
items.update((k + '_1' if k in items else k, v) for k, v in n_items.items())
output:
{'A': 1, 'B': 2, 'C': 3, 'D': 4, 'B_1': 9, 'E': 8, 'C_1': 7}
But I want the output to be like this:
{'A': 1, 'B': 2, 'C': 3, 'D': 4, 'B_1': 4, 'E': 8, 'C_1': 4, 'B_1_1':9,'C_1_1':7}
or like this:
{'A': 1, 'B': 2, 'C': 3, 'D': 4, 'B_1': 4, 'E': 8, 'C_1': 4, 'B_2':9,'C_2':7}
How can I do it ?
You can do this iteratively:
def combine(*args):
result = {}
for d in args:
for key, value in d.items():
key = str(key)
while key in result:
key += '_1'
result[key] = value
return result
print(combine(items, u_items, n_items))
Output:
{'A': 1,
'B': 2,
'C': 3,
'D': 4,
'B_1': 4,
'E': 8,
'C_1': 4,
'C_1_1': 7,
'B_1_1': 9}
Although this seems a bit like an XY Problem, here's an ugly (and I'm pretty sure, inefficient) and also, not very general solution consisting of:
that merges the dictionaries, by appending "_1" to an existing key (doing everything in one line), as you requested, although I'd recommend (since there are cases when the shortest is not necessarily the best):
>>> items = {"A": 1, "B": 2, "C": 3} >>> u_items = {"D": 4, "B": 4, "E": 8, "C": 4} >>> n_items = {"C": 7, "B": 9} >>> >>> items.update((max([k1 + "_1" for k1 in items if k1 == k or k1.startswith(k + "_1")], key=len, default=k), v) for k, v in u_items.items()) >>> items {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'B_1': 4, 'E': 8, 'C_1': 4} >>> >>> items.update((max([k1 + "_1" for k1 in items if k1 == k or k1.startswith(k + "_1")], key=len, default=k), v) for k, v in n_items.items()) >>> items {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'B_1': 4, 'E': 8, 'C_1': 4, 'C_1_1': 7, 'B_1_1': 9} >>> >>> >>> # Merging an additional dictionary ... >>> v_items = {"C": 25} >>> >>> items.update((max([k1 + "_1" for k1 in items if k1 == k or k1.startswith(k + "_1")], key=len, default=k), v) for k, v in v_items.items()) >>> items {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'B_1': 4, 'E': 8, 'C_1': 4, 'C_1_1': 7, 'B_1_1': 9, 'C_1_1_1': 25}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With