Untitled

from header and rows of data, put in a tuple of same related values, and put back in list of dictionary, each key with list of its values
mail@pastecode.io avatar
unknown
python
a year ago
616 B
4
Indexable
Never
th = ["ID", "LABEL", "DESC"]
td = [[1001, "testLabel1", "testDesc1"],[1002, "testLabel2", "testDesc2"]]
result = [{}]

#unpack sublists in td, pass iterables to combine with zip, put in a list
transposed_data = list(zip(*td))
print(transposed_data)
#[(1001, 1002), ('testLabel1', 'testLabel2'), ('testDesc1', 'testDesc2')]
#values uniform in each tuple

#enumerate th to get index and value
for i, header in enumerate(th):
    result[0][header] = list(transposed_data[i])

print(result)
#[{'ID': [1001, 1002], 'LABEL': ['testLabel1', 'testLabel2'], 'DESC': ['testDesc1', 'testDesc2']}]
# each key with related values