I have a nested list holding dictionaries as mapping table using a tuple as key. I am struggling to zip
the dictionary together so it can be exported by Pandas to csv file:
l = [{('A', 'B'): 1}, {('A', 'C'): 2}, {('A', 'D'): 3}]
def dynDictCombiner(item):
# would lambda be faster?
def _combine(item):
key = item.keys()[0]
return key, item[key]
col_keys = ('start', 'stop')
row_keys = ('value')
# syntax error
l = [dict(zip(col_keys + row_keys, k + v)) for ((k, v) :=_combine(item) in l)]
print(l)
l = dynDictCombiner(l)
#import pandas as pd
#df = pd.DataFrame.from_dict(l)
#print(df.shape)
#print(df)
#df.to_csv(path_or_buf='C:/temp/foo.csv', index=False, header=True)
Expected Output:
[
{'start': 'A', 'stop': 'B', 'value': 1},
{'start': 'A', 'stop': 'C', 'value': 2},
{'start': 'A', 'stop': 'D', 'value': 3}
]
Edit, function without walrus:
def dynDictCombinerSimple(items):
# would lambda be faster?
def _combine(item):
key = list(item.keys())[0]
return key, (item[key], )
col_keys = ('start', 'stop')
row_keys = ('value', )
result = []
for item in items:
k, v = _combine(item)
result.append(dict(zip(col_keys + row_keys, k + v)))
print(result)
Out as expected:
[{'start': 'A', 'stop': 'B', 'value': 1}, {'start': 'A', 'stop': 'C', 'value': 2}, {'start': 'A', 'stop': 'D', 'value': 3}]