0

I have the following arrays:

A=[1,2,3]
B=[6,7,8]

I would like to compose them into JSON format, specifically, as an array of objects. I am trying to write a function that will accept multiple arrays (in the example above A and B) and a corresponding number of keys (in this case: :"arrayA", "arrayB"), and will output them as an array of objects below:

[{"arrayA":1, "arrayB":6},{"arrayA":2, "arrayB":7},{"arrayA":3, "arrayB":8}]
3
  • 3
    Same as stackoverflow.com/questions/25348640/… Commented Jul 4, 2016 at 2:36
  • 1
    Can you show us your attempts? Commented Jul 4, 2016 at 2:49
  • I actually think this question is a bit different, since it is asking how to take any number of lists. At least, it is not an exact duplicate. Commented Jul 4, 2016 at 2:55

1 Answer 1

-1

As pointed out below my answer is incorrect because lists do not have names but you could try to combine it with this answer to set up something that aims at what you are trying to do

How to get a variable name as a string in Python?

names = [a.__name__ for a in arrays] 
objs = [] 
For arrs in zip(arrays):
    objs.append({"array_" + n: val for n, val in zip(names, arrs)})
Sign up to request clarification or add additional context in comments.

1 Comment

A list doesn't have a __name__ attribute.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.