3

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?

llllllllll
  • 16,169
  • 4
  • 31
  • 54
leonard vertighel
  • 1,058
  • 1
  • 18
  • 37

1 Answers1

5

You can do this:

t_data = [a, b, c]
[{u:v for u, v in zip(t, xs)} for xs in zip(*t_data)]
llllllllll
  • 16,169
  • 4
  • 31
  • 54
  • Brilliant!!!! Is there also an equivalent expression to obtain the opposite? I.e. from `[ { }, { }, { } ]` to `{'A':[1,2,3 ], 'B' : [4,5,6], 'C' : [ 7,8,9] }` ? – leonard vertighel Mar 21 '18 at 14:25
  • There is no equivalent expression to get back. In fact, this is another irrelevant question. It'd be better to ask another question. – llllllllll Mar 21 '18 at 14:36