With pandas, you can use something like this function
def groupItems(dictList, itemsFrom, groupBy, saveTo=None):
ik, gk, colsDict = itemsFrom, groupBy, {}
groups = {str(d.get(gk)): d.get(gk) for d in dictList}
itemsList = [ [d.get(ik) for d in dictList if str(d.get(gk))==g]
for g in groups ]
maxRows = max(len(li) for li in itemsList) if groups else 0
for gi, (g, li) in enumerate(zip(groups.keys(), itemsList), 1):
colsDict[f'Group {gi}'] = [groups[g]] + [None]*(maxRows-1)
colsDict[f'Group {gi} Items'] = li + [None]*(maxRows-len(li))
rdf = pandas.DataFrame(colsDict)
if saveTo and isinstance(saveTo, str):
print('Saving', maxRows, 'rows for', len(groups),'groups to', saveTo)
rdf.to_csv(saveTo, index=False)
return rdf
Calling groupItems(listofdictionaries, 'key1', 'key2', 'x.csv') will save the DataFrame from the screenshot below to x.csv.
To demonstrate that the brackets were not lost: