2

I am trying to create a list of dictionaries grouping common values in a given data set. The data is formatted as follows

data = [{"CustName":"customer1", "PartNum":"part1"},
{"CustName":"customer2", "PartNum":"part2"}, 
{"CustName":"customer1", "PartNum":"part3"}, 
{"CustName":"customer2", "PartNum":"part4"}]

what I want is

cleanedData = [
{"CustName":"customer1", "parts":[{"PartNum":"part1"}, {"PartNum":"part3"}]},
{"CustName":"customer2", "parts":[{"PartNum":"part2"}, {"PartNum":"part4"}]}]

the way I'm trying to get working requires several loops, looks ugly, and doesn't feel very pythonic. I also feel like this will not scale very well. Currently, the input data is small - less than 100 elements, but potentially this could be thousands of elements, so the multiple loops in loops seems inefficient.

data = [{"CustName":"customer1", "PartNum":"part1"},
{"CustName":"customer2", "PartNum":"part2"}, 
{"CustName":"customer1", "PartNum":"part3"}, 
{"CustName":"customer2", "PartNum":"part4"}]

customers = []
cleanedData = []
for d in data:
    if d["CustName"] not in customers:
        customers.append(d["CustName"])

for c in customers:
    parts = []
    for d in data:
        if d["CustCode"] == c:
            parts.append(d)
    cust = {"CustName":c}
    cust.update({"parts":parts})
    cleanedData.append(cust)

Can someone help and offer a simpler way of doing this? Is there a builtin function that helps with this kind of data manipulation?

guidoc
  • 105
  • 6
  • I know this isn't what you want, but would the answer of `{'customer1': ['part1', 'part3'], 'customer2': ['part2', 'part4']}` be acceptable? Or do you need the exact data you want? – TerryA Dec 29 '15 at 03:26
  • I need the key value pairs for other parts of the program. The solution below does exactly that with the list comprehension. – guidoc Dec 29 '15 at 21:44

1 Answers1

2

You can use collections.defaultdict.

d = defaultdict(list)
for item in data:
    d[item['CustName']].append({'PartNum': item['PartNum']})
print(d)

And optionally following list comprehension if you want it in the list:

print([{'CustName': key, 'parts': value} for key, value in d.items()])    
Yaroslav Admin
  • 13,880
  • 6
  • 63
  • 83
  • 1
    Excellent! Worked perfect. Need to make sure you import `from collections import defaultdict` – guidoc Dec 29 '15 at 21:43