I'm trying to merge two arrays and place thier respective values next to each other. For example, have those two arrays.
Arry1 = [{"key" : "A", "values": [[111], [222]]}]
Arry2 = [333,444,555]
I'd like to marge them and have the output look like this.
output = [{"key" : "A", "values": [[111, 333], [222,444]]}]
I tried this method, however the output contains extra unacecery arrays within the main array.
out = []
for t in zip(Arry1[0]["values"],Arry2):
for x in t:
out.append(x)
#output [[111], [333], [222], [444]]
CodePudding user response:
I hate to add another answer when the previous two do the job, but I think that the neatest answer leverage's python's zip
function:
Arry1 = [{"key" : "A", "values": [[111], [222]]}]
Arry2 = [333,444,555]
for x, y in zip(Arry1[0]["values"], Arry2):
x.append(y)
CodePudding user response:
Please fix your brackets, Also does this work for you? Considering I got the brackets right...
Arry1 = [{"key" : "A", "values": [[111], [222]]}]
Arry2 = [333,444,555]
for arr in Arry1[0].values():
index = 0
if type(arr) == list:
for arr2 in arr:
arr2.append(Arry2[index])
index = 1
print(Arry1)
Output
[{'key': 'A', 'values': [[111, 333], [222, 444]]}]
CodePudding user response:
I can't quite work out what you want, but i think this solves it(?)
Arry1 = [{"key" : "A", "values": [[111], [222]]}]
Arry2 = [333,444,555]
for i in range(len(Arry1[0]["values"])):
Arry1[0]["values"][i].append(Arry2[i])
print(Arry1)
This prints out "[{'key': 'A', 'values': [[111, 333], [222, 444]]}]"