Check whether an entry present in python list and add the elements
Question: I have two python list of the form

    
    
    list1 = [('TGFB1', 'TGFB1', 1), ('TGFB1', 'CRP', 0.4),('BRCA2', 'TP53', 0.3)]
    list2 = [('BRCA1', 'TP53', 2), ('TGFB1', 'CRP', 0.4),('BRCA2', 'TP53', 0.3)]
    

I need to check whether each entry in list2 is present in list1 . If present
add the integer part and store in a new list. If not present just append that
entry to the newly created list. So here my newly created list3 should look
like

    
    
    list3 = [('TGFB1', 'TGFB1', 1), ('TGFB1', 'CRP', 0.8),('BRCA2', 'TP53', 0.6),('BRCA1', 'TP53', 2) ]
    


Answer: Following my above comment, this is what you might want:

    
    
    from copy import copy
    
    d3 = copy(dict(((x, y), z) for x, y, z in list1))
    d2 = dict(((x, y), z) for x, y, z in list2)
    
    for key, value in d2.iteritems():
        d3[key] = (d3[key] if key in d3 else 0.0) + value
    

If you do really want to turn `d3` then back to a list of the same structure
use:

    
    
    list3 = [key + (value,) for key, value in d3.iteritems()]
    

