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?