Search code examples
pythonarraysdictionaryelementwise-operations

Create element wise dictionary from python arrays


From these arrays:

t = ["A","B","C"]
a = [1,2,3]
b = [4,5,6]
c = [7,8,9]

how can I obtain a list like this

[
  { 'A':1, 'B':4, 'C':7},
  { 'A':2, 'B':5, 'C':8},
  { 'A':3, 'B':6, 'C':9},
]

so that it is more useful one dumped in JSON?


Solution

  • You can do this:

    t_data = [a, b, c]
    [{u:v for u, v in zip(t, xs)} for xs in zip(*t_data)]