dkeeper09
dkeeper09

Reputation: 547

Combining list of lists with dictionaries

Thank you to all who help here.

I have a list of lists. Those lists contain dictionaries like so:

combined lists = [
        [
            {'COMPANY': 'company1', 'NUMBER': '111', 'SHIPMENTS': ['1', '2', '3', '4']},
            {'COMPANY': 'company2', 'NUMBER': '222', 'SHIPMENTS': ['1']},
            {'COMPANY': 'company3', 'NUMBER': '333', 'SHIPMENTS': ['1', '4']},
            {'COMPANY': 'company4', 'NUMBER': '444', 'SHIPMENTS': ['2', '5']},
            {'COMPANY': 'company5', 'NUMBER': '555', 'SHIPMENTS': ['1', '3', '5', '9']}
        ], 
        [
            {'COMPANY': 'company1', 'NUMBER': '111', 'SHIPMENTS': ['5', '6', '7', '8']},
            {'COMPANY': 'company3', 'NUMBER': '333', 'SHIPMENTS': ['3', '5']},
            {'COMPANY': 'company5', 'NUMBER': '555', 'SHIPMENTS': ['3', '5', '7']},
            {'COMPANY': 'company7', 'NUMBER': '777', 'SHIPMENTS': ['2', '4']},
            {'COMPANY': 'company9', 'NUMBER': '999', 'SHIPMENTS': ['1', '2', '5', '6', '7']}
        ], 
    ]

I to combine these lists based on the COMPANY and SHIPMENTS, and I'd like to not have duplicate SHIPMENTS values. The NUMBER key/value is irrelevant.

Final output would ideally be a list of dictionaries that looks something like this, where the shipments are all combined for the company:

final_list = [
        {'COMPANY': 'company1', 'SHIPMENTS': ['1', '2', '3', '4', '5', '6', '7', '8']},
        {'COMPANY': 'company2', 'SHIPMENTS': ['1']},
        {'COMPANY': 'company3', 'SHIPMENTS': ['1', '4', '3', '5']},
        {'COMPANY': 'company4', 'SHIPMENTS': ['2', '5']},
        {'COMPANY': 'company5', 'SHIPMENTS': ['1', '3', '5', '7', '9']},
        {'COMPANY': 'company7', 'SHIPMENTS': ['2', '4']},
        {'COMPANY': 'company9', 'SHIPMENTS': ['1', '2', '5', '6', '7']}
    ]

I know I haven't offered anything I've tried, but mainly looking for how to approach getting to the final output. I'm using python3.6 if that matters

Upvotes: 0

Views: 60

Answers (3)

null
null

Reputation: 2137

This solves the problem, try it and play around with it, you can optimize the code for better performance.

def company_exists(company, resulting_list):
    for i,dict_ in enumerate(resulting_list):
        if company == dict_['COMPANY']:
            return i, True
    return None, False


def merge_lists(combined_lists):
    res = []

    for list_ in combined_lists:
        for dict_ in list_:
            idx, check = company_exists(dict_['COMPANY'], res)
            if not check:
                res.append(dict_)
            else:
                res[idx]['SHIPMENTS'].extend(dict_['SHIPMENTS'])
                res[idx]['SHIPMENTS'] = list(set(res[idx]['SHIPMENTS']))

    return res

Hope, it helpes.

Upvotes: 0

Dhruv Agarwal
Dhruv Agarwal

Reputation: 558

I think this should solve your issue

import collections

merged = collections.defaultdict(list)

for x in combined_lists:
    for y in x:
        merged[y["COMPANY"]] += y["SHIPMENT"]
final_list = []
for x in merged:
    final_list.append({"COMPANY": x, "SHIPMENT": merged[x]})

Upvotes: 0

Grismar
Grismar

Reputation: 31319

Here's a solution, this uses sets to ensure there are no duplicates, but it will lose the order of shipments.

from itertools import chain

combined_lists = [
    [
        {'COMPANY': 'company1', 'NUMBER': '111', 'SHIPMENTS': ['1', '2', '3', '4']},
        {'COMPANY': 'company2', 'NUMBER': '222', 'SHIPMENTS': ['1']},
        {'COMPANY': 'company3', 'NUMBER': '333', 'SHIPMENTS': ['1', '4']},
        {'COMPANY': 'company4', 'NUMBER': '444', 'SHIPMENTS': ['2', '5']},
        {'COMPANY': 'company5', 'NUMBER': '555', 'SHIPMENTS': ['1', '3', '5', '9']}
    ],
    [
        {'COMPANY': 'company1', 'NUMBER': '111', 'SHIPMENTS': ['5', '6', '7', '8']},
        {'COMPANY': 'company3', 'NUMBER': '333', 'SHIPMENTS': ['3', '5']},
        {'COMPANY': 'company5', 'NUMBER': '555', 'SHIPMENTS': ['3', '5', '7']},
        {'COMPANY': 'company7', 'NUMBER': '777', 'SHIPMENTS': ['2', '4']},
        {'COMPANY': 'company9', 'NUMBER': '999', 'SHIPMENTS': ['1', '2', '5', '6', '7']}
    ]
]

COMPANY_KEY = 'COMPANY'
SHIPMENTS_KEY = 'SHIPMENTS'

# you're looking to:
# - combine the lists
# - drop the number
# - combine the shipments, removing duplicates
final_dict = {}
for d in chain.from_iterable(combined_lists):
    key = d[COMPANY_KEY]
    if key in final_dict:
        final_dict[key][SHIPMENTS_KEY].update(*d[SHIPMENTS_KEY])
    else:
        final_dict[key] = {SHIPMENTS_KEY: set(d[SHIPMENTS_KEY])}
print(final_dict)

# if you need a list, not a dict
final_list = [{COMPANY_KEY: key, SHIPMENTS_KEY: value} for key, value in final_dict.items()]
print(final_list)

Note that, if all you need is a list of shipments and that's really the only thing in your dictionaries, an even simpler solution would be this:

from collections import defaultdict

better_dict = defaultdict(set)
for d in chain.from_iterable(combined_lists):
    better_dict[d[COMPANY_KEY]].update(*d[SHIPMENTS_KEY])
print(better_dict)

Upvotes: 1

Related Questions