0

I have two dictionaries of the type:

dict1 = {NTID: (ID, CN)}

dict2 = {NTID: INCHI}

and I want to get:

dict3 = {NTID: (ID, CN, INCHI)}

I have tried to use zip, as follows:

dict_3 = dict(zip(dict1, dict2))

BUt I get NTID as keys and values. Please note that there are multiple values per each dictionary, but the order of the values between dict1 and dict2 is consistent

EDIT:

An example from dict1 is (keys: '1-4-HYDROXYPHENYL', '4-AMINO-BUTYRATE', 'ACETYLCHOLINE', 'ASN')

{'1-4-HYDROXYPHENYL': [],
 '4-AMINO-BUTYRATE': [('24', 'gamma-aminobutyrate (GABA)'),
  ('172', 'alpha-ketoglutarate'),
  ('173', 'succinate'),
  ('399', 'Succinate semialdehyde'),
  ('111', 'putrescine'),
  ('114', '4-guanidinobutanoate'),
  ('115', '4-acetamidobutanoate')],
 'ACETYLCHOLINE': [],
 'ASN': [('1', 'glycine'),
  ('45', 'tyrosine'),
  ('74', 'leucine'),
  ('90', 'methionine'),
  ('80', 'valine')]}

dict2:

{'1-4-HYDROXYPHENYL': [],
 '4-AMINO-BUTYRATE': ['InChI=1S/C4H9NO2/c5-3-1-2-4(6)7/h1-3,5H2,(H,6,7)',
  'InChI=1S/C5H6O5/c6-3(5(9)10)1-2-4(7)8/h1-2H2,(H,7,8)(H,9,10)/p-2',
  'InChI=1S/C4H6O4/c5-3(6)1-2-4(7)8/h1-2H2,(H,5,6)(H,7,8)/p-2',
  'InChI=1S/C4H6O3/c5-3-1-2-4(6)7/h3H,1-2H2,(H,6,7)/p-1',
  'InChI=1S/C4H12N2/c5-3-1-2-4-6/h1-6H2/p+2',
  'InChI=1S/C5H11N3O2/c6-5(7)8-3-1-2-4(9)10/h1-3H2,(H,9,10)(H4,6,7,8)',
  'InChI=1S/C6H11NO3/c1-5(8)7-4-2-3-6(9)10/h2-4H2,1H3,(H,7,8)(H,9,10)/p-1'],
 'ACETYLCHOLINE': [],
 'ASN': ['InChI=1S/C9H11NO3',
  'InChI=1S/C9H11NO2/c10-8',
  'InChI=1S/C3H7NO2/c1-2(4)3(5)6',
  'InChI=1S/C5H9NO2/c7-5(8)4-2-1-3',
  'InChI=1S/C4H9NO3/c1-2(6)3',
  'InChI=1S/C4H8N2O3/c5-2(4(8)',
  'InChI=1S/C6H13NO2/c1-3-4(2)',
  'InChI=1S/C6H9N3O2/c7-5(6(10)',
  'InChI=1S/C4H7NO4/c5-2(4(8)9)',
  'InChI=1S/C4H4O5/c5-2(4(8)9)1-',
  'InChI=1S/C3H7NO2S/c4-2(1-7)3(5)',
  'InChI=1S/C5H10N2O3/c6-3(5(9)'}

dict3 (output) should look like:

{'1-4-HYDROXYPHENYL': [],
 '4-AMINO-BUTYRATE': [('24', 'gamma-aminobutyrate (GABA)', 'InChI=1S/C4H9NO2/c5-3-1-2-4(6)7/h1-3,5H2,(H,6,7)'),
  ('172', 'alpha-ketoglutarate', 'InChI=1S/C5H6O5/c6-3(5(9)10)1-2-4(7)8/h1-2H2,(H,7,8)(H,9,10)/p-2', 'InChI=1S/C4H6O4/c5-3(6)1-2-4(7)8/h1-2H2,(H,5,6)(H,7,8)/p-2'),
  ('173', 'succinate', 'InChI=1S/C4H6O3/c5-3-1-2-4(6)7/h3H,1-2H2,(H,6,7)/p-1'),
  etc...
StudentOIST
  • 189
  • 2
  • 7
  • 21
  • 4
    Possible duplicate of [How to merge multiple dicts with same key?](https://stackoverflow.com/questions/5946236/how-to-merge-multiple-dicts-with-same-key) – eugenhu Nov 17 '17 at 06:29
  • `dict1['NTID'] += (dict2['NTID'],)`? – timgeb Nov 17 '17 at 06:30
  • Should I put that in a for loop, to loop through each keys? – StudentOIST Nov 17 '17 at 06:33
  • I have seen that post, but my post is not really the same case, because the value of one of my dictionary is a tuple, and I want my output dictionary to have tuples of three elements as value – StudentOIST Nov 17 '17 at 06:37
  • [One of the answers](https://stackoverflow.com/a/5946322/8944057) there should give you an idea of how you would approach this. I'm not sure if there's a better way but the lack of better answers seems to suggest that iterating through both dictionaries is the best approach. Referring to the linked answer, for your specific case since `dict1` stores tuples, instead of `.append(value)`, you can use `.extend(value)` for when value is a tuple. – eugenhu Nov 17 '17 at 07:11

3 Answers3

1

You can try this code:

dict1 = {'NTID': ('ID', 'CN')}

dict2 = {'NTID': 'INCHI'}


    final_dict={}
    for key,value in dict1.items():
        final_dict[key]=[value[0],value[1]]
        if key in dict2:
            final_dict[key].append(dict2.get(key))

    print(final_dict)

output:

{'NTID': ['ID', 'CN', 'INCHI']}

if you want result in tuple as you shown then:

final_dict={}
for key,value in dict1.items():
    final_dict[key]=value
    if key in dict2:
        final_dict[key]+=(dict2.get(key),)

print(final_dict)

output:

{'NTID': ('ID', 'CN', 'INCHI')}

second you can use defaultdict:

dict1 = {'NTID': ('ID', 'CN')}

dict2 = {'NTID': 'INCHI'}

from collections import defaultdict

d=defaultdict(list)
for item in (dict1,dict2):
    for key,value in item.items():
        d[key].append(value)

print(d)

Updated as per question edit :

dict_1={'1-4-HYDROXYPHENYL': [],
 '4-AMINO-BUTYRATE': [('24', 'gamma-aminobutyrate (GABA)'),
  ('172', 'alpha-ketoglutarate'),
  ('173', 'succinate'),
  ('399', 'Succinate semialdehyde'),
  ('111', 'putrescine'),
  ('114', '4-guanidinobutanoate'),
  ('115', '4-acetamidobutanoate')],
 'ACETYLCHOLINE': [],
 'ASN': [('1', 'glycine'),
  ('45', 'tyrosine'),
  ('74', 'leucine'),
  ('90', 'methionine'),
  ('80', 'valine')]}



dict_2={'1-4-HYDROXYPHENYL': [],
 '4-AMINO-BUTYRATE': ['InChI=1S/C4H9NO2/c5-3-1-2-4(6)7/h1-3,5H2,(H,6,7)',
  'InChI=1S/C5H6O5/c6-3(5(9)10)1-2-4(7)8/h1-2H2,(H,7,8)(H,9,10)/p-2',
  'InChI=1S/C4H6O4/c5-3(6)1-2-4(7)8/h1-2H2,(H,5,6)(H,7,8)/p-2',
  'InChI=1S/C4H6O3/c5-3-1-2-4(6)7/h3H,1-2H2,(H,6,7)/p-1',
  'InChI=1S/C4H12N2/c5-3-1-2-4-6/h1-6H2/p+2',
  'InChI=1S/C5H11N3O2/c6-5(7)8-3-1-2-4(9)10/h1-3H2,(H,9,10)(H4,6,7,8)',
  'InChI=1S/C6H11NO3/c1-5(8)7-4-2-3-6(9)10/h2-4H2,1H3,(H,7,8)(H,9,10)/p-1'],
 'ACETYLCHOLINE': [],
 'ASN': ['InChI=1S/C9H11NO3',
  'InChI=1S/C9H11NO2/c10-8',
  'InChI=1S/C3H7NO2/c1-2(4)3(5)6',
  'InChI=1S/C5H9NO2/c7-5(8)4-2-1-3',
  'InChI=1S/C4H9NO3/c1-2(6)3',
  'InChI=1S/C4H8N2O3/c5-2(4(8)',
  'InChI=1S/C6H13NO2/c1-3-4(2)',
  'InChI=1S/C6H9N3O2/c7-5(6(10)',
  'InChI=1S/C4H7NO4/c5-2(4(8)9)',
  'InChI=1S/C4H4O5/c5-2(4(8)9)1-',
  'InChI=1S/C3H7NO2S/c4-2(1-7)3(5)',
  'InChI=1S/C5H10N2O3/c6-3(5(9)']}


final_dict={}
for key,value in dict_1.items():
    if key in dict_2:
        for zip_value in zip(value,dict_2.get(key)):
            if key not in final_dict:
                final_dict[key]=[zip_value]
            else:
                final_dict[key].append(zip_value)



print(final_dict)

output:

{'ASN': [(('1', 'glycine'), 'InChI=1S/C9H11NO3'), (('45', 'tyrosine'), 'InChI=1S/C9H11NO2/c10-8'), (('74', 'leucine'), 'InChI=1S/C3H7NO2/c1-2(4)3(5)6'), (('90', 'methionine'), 'InChI=1S/C5H9NO2/c7-5(8)4-2-1-3'), (('80', 'valine'), 'InChI=1S/C4H9NO3/c1-2(6)3')], '4-AMINO-BUTYRATE': [(('24', 'gamma-aminobutyrate (GABA)'), 'InChI=1S/C4H9NO2/c5-3-1-2-4(6)7/h1-3,5H2,(H,6,7)'), (('172', 'alpha-ketoglutarate'), 'InChI=1S/C5H6O5/c6-3(5(9)10)1-2-4(7)8/h1-2H2,(H,7,8)(H,9,10)/p-2'), (('173', 'succinate'), 'InChI=1S/C4H6O4/c5-3(6)1-2-4(7)8/h1-2H2,(H,5,6)(H,7,8)/p-2'), (('399', 'Succinate semialdehyde'), 'InChI=1S/C4H6O3/c5-3-1-2-4(6)7/h3H,1-2H2,(H,6,7)/p-1'), (('111', 'putrescine'), 'InChI=1S/C4H12N2/c5-3-1-2-4-6/h1-6H2/p+2'), (('114', '4-guanidinobutanoate'), 'InChI=1S/C5H11N3O2/c6-5(7)8-3-1-2-4(9)10/h1-3H2,(H,9,10)(H4,6,7,8)'), (('115', '4-acetamidobutanoate'), 'InChI=1S/C6H11NO3/c1-5(8)7-4-2-3-6(9)10/h2-4H2,1H3,(H,7,8)(H,9,10)/p-1')]}

Little modifed if you don't want those extra brackets :

final_dict={}
for key,value in dict_1.items():
    if key in dict_2:
        for zip_value in zip(value,dict_2.get(key)):

            if key not in final_dict:
                final_dict[key]=[zip_value[0][0],zip_value[0][1],zip_value[1]]
            else:
                final_dict[key].append((zip_value[0][0],zip_value[0][1],zip_value[1]))



print(final_dict)

output:

{'ASN': ['1', 'glycine', 'InChI=1S/C9H11NO3', ('45', 'tyrosine', 'InChI=1S/C9H11NO2/c10-8'), ('74', 'leucine', 'InChI=1S/C3H7NO2/c1-2(4)3(5)6'), ('90', 'methionine', 'InChI=1S/C5H9NO2/c7-5(8)4-2-1-3'), ('80', 'valine', 'InChI=1S/C4H9NO3/c1-2(6)3')], '4-AMINO-BUTYRATE': ['24', 'gamma-aminobutyrate (GABA)', 'InChI=1S/C4H9NO2/c5-3-1-2-4(6)7/h1-3,5H2,(H,6,7)', ('172', 'alpha-ketoglutarate', 'InChI=1S/C5H6O5/c6-3(5(9)10)1-2-4(7)8/h1-2H2,(H,7,8)(H,9,10)/p-2'), ('173', 'succinate', 'InChI=1S/C4H6O4/c5-3(6)1-2-4(7)8/h1-2H2,(H,5,6)(H,7,8)/p-2'), ('399', 'Succinate semialdehyde', 'InChI=1S/C4H6O3/c5-3-1-2-4(6)7/h3H,1-2H2,(H,6,7)/p-1'), ('111', 'putrescine', 'InChI=1S/C4H12N2/c5-3-1-2-4-6/h1-6H2/p+2'), ('114', '4-guanidinobutanoate', 'InChI=1S/C5H11N3O2/c6-5(7)8-3-1-2-4(9)10/h1-3H2,(H,9,10)(H4,6,7,8)'), ('115', '4-acetamidobutanoate', 'InChI=1S/C6H11NO3/c1-5(8)7-4-2-3-6(9)10/h2-4H2,1H3,(H,7,8)(H,9,10)/p-1')]}
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88
0

You can try this:

dict3 = {name: [(*a, b) for a, b in zip(v, dict2[name])]
    for name, v in dict1.items()
}

From what I understand, dict1 and dict2 have the same keys and structure but are storing different information related to the same one thing, and you want to merge them both into a common dict3.

eugenhu
  • 1,168
  • 13
  • 22
0
dict3 = dict(zip(dict1.keys(), zip(dict1.values(),dict2.values())))
Mr Ed
  • 83
  • 2
  • 12
  • 4
    Code is always good, but it also helps to add some comments or context around how the code addresses the original question. – craigcaulfield Sep 20 '18 at 03:49