I have two dictionaries (dict1, dict2) with 100 same keys but different lists of tuples as values:
dict1 = {'M1': [(50, 'M'), (4, 'K')], 'N2': [(500, 'N'), (3, 'C'), (7, 'K')], 'S3': ...}
dict2 = {'M1': [(46, 'M'), (2, 'K'), (11, 'F')], 'N2': [(400, 'N'), (5, 'C')], 'S3': ...}
I want to create a new dictionary (dict3) in which the values are added if they have the same letter in first position of tuple and if not the tuple has to be added to the values of the new dictionary:
dict3 = {'M1': [(96, 'M'), (6, 'K'), (11, 'F')], 'N2': [(900, 'N'), (8, 'C'), (7, 'K)], 'S3': ...}
I was thinking about something like this with python3 (this code does not work):
dict1 = {'M1': [(50, 'M'), (4, 'K')], 'N2': [(500, 'N'), (3, 'C'), (7, 'K')]}
dict2 = {'M1': [(46, 'M'), (2, 'K'), (11, 'F')], 'N2': [(400, 'N'), (5, 'C')]}
dict3 = {}
for (val1, key1), (val2, key2) in zip(dict1, dict2):
if t1[1]==t2[1] for (t1, t2) in (key1, key2):
t3_0 = t1[0] + t2[0]
t3_1 = t1[1]
elif t1[1] not in t2[1]:
t3_0 = t1[0]
t3_1 = t1[1]
elif t2[1] not in t1[1]
t3_0 = t2[0]
t3_1 = t2[1]
key3 = [(t3_0, t3_1)]
# here val1=val2
dict3[val1] = key3
I would appreciate your help very much. Thank you.
zip(dict1, dict2)
will iterate over the keys of each dictionary, which are strings and thus cannot be unpacked with(val1, key1)
. – ForceBru