Merging similar dictionaries in a list together

Posted by WonderSteve on Stack Overflow See other posts from Stack Overflow or by WonderSteve
Published on 2012-10-05T21:09:34Z Indexed on 2012/10/05 21:37 UTC
Read the original article Hit count: 179

Filed under:
|

New to python here. I've been pulling my hair for hours and still can't figure this out.

I have a list of dictionaries:

[ {'FX0XST001.MID5': '195', 'Name': 'Firmicutes', 'Taxonomy ID': '1239', 'Type': 'phylum'}
  {'FX0XST001.MID13': '4929', 'Name': 'Firmicutes', 'Taxonomy ID': '1239','Type': 'phylum'},
  {'FX0XST001.MID6': '826', 'Name': 'Firmicutes', 'Taxonomy ID': '1239', 'Type': 'phylum'},
                                        .
                                        .
                                        .
                                        .

  {'FX0XST001.MID6': '125', 'Name': 'Acidobacteria', 'Taxonomy ID': '57723', 'Type': 'phylum'}
  {'FX0XST001.MID25': '70', 'Name': 'Acidobacteria', 'Taxonomy ID': '57723', 'Type': 'phylum'}
  {'FX0XST001.MID40': '40', 'Name': 'Acidobacteria', 'Taxonomy ID': '57723', 'Type': 'phylum'} ]

I want to merge the dictionaries in the list based on their Type, Name, and Taxonomy ID

  [ {'FX0XST001.MID5': '195', 'FX0XST001.MID13': '4929', 'FX0XST001.MID6': '826', 'Name': 'Firmicutes', 'Taxonomy ID': '1239', 'Type': 'phylum'}
                                        .
                                        .
                                        .
                                        .

    {'FX0XST001.MID6': '125', 'FX0XST001.MID25': '70', 'FX0XST001.MID40': '40', 'Name': 'Acidobacteria', 'Taxonomy ID': '57723', 'Type': 'phylum'}]

I have the data structure setup like this because I need to write the data to CSV using csv.DictWriter later.

Would anyone kindly point me to the right direction?

© Stack Overflow or respective owner

Related posts about python

Related posts about dictionary