Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

convert list of tuples to lists of dicts with whole tuples as keys

I have a nested list of tuples as given below,

>>> my_nested_list_of_tuples
  [[('AU041133(+)', 'Qtrt1'), ('Ahr(+)', 'Mex3c'), ('Arid3a(+)', 'Hmgb1')],     
  [('AU041133(+)', 'Topors'), ('Ahr(+)', 'Mex3c'), ('Arid3a(+)', 'Hmgb1')],   
  [('AU041133(+)', 'Tm9sf3'), ('Ahr(+)', 'Mex3c'), ('Arid3a(+)', 'Hmgb1')], 
  [('AU041133(+)', 'Gsn'), ('Ahr(+)', 'Ireb2'), ('Arid3a(+)', 'Hmgb1')],
  [('AU041133(+)', 'Tm9sf3'), ('Ahr(+)', 'Ilf3'), ('Arid3a(+)', 'Arid3a')]]

I want to convert the above nested list of tuples to list of dictionaries with whole tuple as key and 0 as value as given below

 [{('AU041133(+)', 'Qtrt1'): 0, ('Ahr(+)', 'Mex3c'): 0, ('Arid3a(+)', 'Hmgb1'): 0},
 {('AU041133(+)', 'Topors'): 0, ('Ahr(+)', 'Mex3c'): 0, ('Arid3a(+)', 'Hmgb1'): 0},  
 {('AU041133(+)', 'Tm9sf3'): 0, ('Ahr(+)', 'Mex3c'): 0, ('Arid3a(+)', 'Hmgb1'): 0}, 
 {('AU041133(+)', 'Gsn'): 0, ('Ahr(+)', 'Ireb2'): 0, ('Arid3a(+)', 'Hmgb1'): 0}, 
 {('AU041133(+)', 'Tm9sf3'): 0, ('Ahr(+)', 'Ilf3'): 0, ('Arid3a(+)', 'Arid3a'): 0}] 

I have tried various methods for achieving this but I am not successful in doing so, one of the way I tried is given below,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>>> d = defaultdict(list)
>>> dk=[] # for populating with list of dicts
>>> for k in my_tops: 
...     for i,v in k:
...          d[(i,v)].append(0)
...     dk.append(d)
... 

>>> dk
[defaultdict(<class 'list'>, {('AU041133(+)', 'Qtrt1'): [0], ('Ahr(+)', 'Mex3c'):  [0, 0, 0], 
('Arid3a(+)', 'Hmgb1'): [0, 0, 0, 0], ('AU041133(+)', 'Topors'): [0], ('AU041133(+)', 'Tm9sf3'): [0, 0], 
('AU041133(+)', 'Gsn'): [0], ('Ahr(+)', 'Ireb2'): [0], ('Ahr(+)', 'Ilf3'): [0], ('Arid3a(+)', 'Arid3a'): [0]}), 
defaultdict(<class 'list'>, {('AU041133(+)', 'Qtrt1'): [0], ('Ahr(+)', 'Mex3c'): [0, 0, 0], 
('Arid3a(+)', 'Hmgb1'): [0, 0, 0, 0], ('AU041133(+)', 'Topors'): [0],  ('AU041133(+)', 'Tm9sf3'): [0, 0], 
('AU041133(+)', 'Gsn'): [0], ('Ahr(+)', 'Ireb2'): [0], ('Ahr(+)', 'Ilf3'): [0],  ('Arid3a(+)', 'Arid3a'): [0]}), 
defaultdict(<class 'list'>, {('AU041133(+)', 'Qtrt1'): [0], ('Ahr(+)', 'Mex3c'): [0, 0, 0], 
('Arid3a(+)', 'Hmgb1'): [0, 0, 0, 0], ('AU041133(+)', 'Topors'): [0], ('AU041133(+)', 'Tm9sf3'): [0, 0], 
('AU041133(+)', 'Gsn'): [0], ('Ahr(+)', 'Ireb2'): [0], ('Ahr(+)', 'Ilf3'): [0], ('Arid3a(+)', 'Arid3a'): [0]}), 
defaultdict(<class 'list'>, {('AU041133(+)', 'Qtrt1'): [0], ('Ahr(+)', 'Mex3c'): [0, 0, 0], 
 ('Arid3a(+)', 'Hmgb1'): [0, 0, 0, 0], ('AU041133(+)', 'Topors'): [0], ('AU041133(+)', 'Tm9sf3'): [0, 0], 
('AU041133(+)', 'Gsn'): [0], ('Ahr(+)', 'Ireb2'): [0], ('Ahr(+)', 'Ilf3'): [0], ('Arid3a(+)', 'Arid3a'): [0]}), 
defaultdict(<class 'list'>, {('AU041133(+)', 'Qtrt1'): [0], ('Ahr(+)', 'Mex3c'): [0, 0, 0], ('Arid3a(+)', 'Hmgb1'): [0, 0, 0, 0], 
('AU041133(+)', 'Topors'): [0], ('AU041133(+)', 'Tm9sf3'): [0, 0], 
('AU041133(+)', 'Gsn'): [0], ('Ahr(+)', 'Ireb2'): [0], ('Ahr(+)', 'Ilf3'): [0],  ('Arid3a(+)', 'Arid3a'): [0]})]

Is there a way to get the expected output as given in the second chunk of code?

Thanks,

>Solution :

If I understood correctly your question, the solution could be:

dict_list=[]
>>> for row in my_nested_list_of_tuples:
...     dict_list.append({key:0 for key in row})

[{('AU041133(+)', 'Qtrt1'): 0, ('Ahr(+)', 'Mex3c'): 0, ('Arid3a(+)', 'Hmgb1'): 0},
 {('AU041133(+)', 'Topors'): 0, ('Ahr(+)', 'Mex3c'): 0, ('Arid3a(+)', 'Hmgb1'): 0},
 {('AU041133(+)', 'Tm9sf3'): 0, ('Ahr(+)', 'Mex3c'): 0, ('Arid3a(+)', 'Hmgb1'): 0},
 {('AU041133(+)', 'Gsn'): 0, ('Ahr(+)', 'Ireb2'): 0, ('Arid3a(+)', 'Hmgb1'): 0},
 {('AU041133(+)', 'Tm9sf3'): 0, ('Ahr(+)', 'Ilf3'): 0, ('Arid3a(+)', 'Arid3a'): 0}]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading