I receive data from an internal interface that comes as a list of dicts where multiple of those dicts represent a data-record if they where combined.
Data looks similar to this:
# received data contains 'duplicate' dict-keys
DATA = [
{"ID": 1234},
{"PRICE": 77.33},
{"DATE": "20201222"},
{"ID": 4567},
{"PRICE": 100.99},
{"DATE": "20201222"}
]
In the above example, a "complete" record would contain the dicts ID
, PRICE
and DATE
.
Unfortunately the dict-keys exists multiple times so when I try something like this:
result = {}
for row in DATA:
for idx, val in row.items():
result[idx] = val
print(result)
# {
# 'ID': 4567,
# 'PRICE': 100.99,
# 'DATE': '20201222'
# }
The dict-keys (obviously) overwrite themselves.
I can't find a solution on how to combine the data into this desired structure:
DESIRED = [
{
"ID": 1234,
"PRICE": 77.33,
"DATE": "20201222"
},
{
"ID": 4567,
"PRICE": 100.99,
"DATE": "20201222"
}
]
Any hints for this? I'm even unsure on how to search for a solution to be honest.