Search code examples
pythonlistdictionarydatasetpython-itertools

how to merge a list of dicts based on one key python


i have this list of dicts:

list1 = [
    {'id': 1, 'fop': 192, 'fop_plan': 'capo', 'fup_comments': None},
    {'id': 1, 'fop': 222, 'fop_plan': 'groso', 'fup_comments': None},
    {'id': 2, 'fop': 222, 'fop_plan': 'bien', 'fup_comments': None},
    {'id': 2, 'fop': 222, 'fop_plan': 'bien', 'fup_comments': None},
    {'id': 3, 'fop': 223, 'fop_plan': 'bien', 'fup_comments': None}
]

and i want to get this:

list2 = [
    {'id': 1, 'fop': [192, 222] 'fop_plan': ['capo', 'groso'], 'fup_comments': [None, None]},
    {'id': 2, 'fop': [222, 222], 'fop_plan': ['bien', 'bien'], 'fup_comments': [None, None]},
    {'id': 3, 'fop': 223, 'fop_plan': 'bien', 'fup_comments': None}
]

Solution

  • Something along the following lines will work. This uses itertools.groupby (assuming the data is sorted by id), operator.itemgetter and dict.setdefault for some convenience:

    from operator import itemgetter
    from itertools import groupby
    
    list2 = []
    
    for k, g in groupby(list1, key=itemgetter("id")):
        new_d = {"id": k}
        for d in g:
            for k, v in d.items():
                if k != "id":
                    new_d.setdefault(k, []).append(v)
        list2.append(new_d)
    
    # [{'fop': [192, 222],
    #   'fop_plan': ['capo', 'groso'],
    #   'fup_comments': [None, None],
    #   'id': 1},
    #  {'fop': [222, 222],
    #   'fop_plan': ['bien', 'bien'],
    #   'fup_comments': [None, None],
    #   'id': 2},
    #  {'fop': [223], 'fop_plan': ['bien'], 'fup_comments': [None], 'id': 3}]