1

I have 3 different arrays that I want to join in one array in a specified format
Example:

arr_1 = [A,B,C]
arr_2 = [1,2,3]
arr_3 = [E,F,G]

I want the result array to look like this:

arr_result = [[A,1,E],[B,2,F],[C,3,G]]

4 Answers 4

1

There are some ways you can achieve this of of them is to use zip function here is an example

arr_1 = ['A', 'B', 'C']
arr_2 = [1, 2, 3]
arr_3 = ['E', 'F', 'G']

arr_result = []

for a, b, c in zip(arr_1, arr_2, arr_3):
    arr_result.append([a, b, c])

print(arr_result)

in this example, the code will iterate over the elements of each array and create a new array with the elements from each array. This code assumes that arrays are same length.

another way is using list comprehension

arr_1 = ['A', 'B', 'C']
arr_2 = [1, 2, 3]
arr_3 = ['E', 'F', 'G']

arr_result = [[a, b, c] for a, b, c in zip(arr_1, arr_2, arr_3)]

print(arr_result)
Sign up to request clarification or add additional context in comments.

Comments

0

This list generator should do the trick:

arr_1 = ['A','B','C']
arr_2 = [1,2,3]
arr_3 = ['E','F','G']

arrays = [arr_1,arr_2,arr_3]
print([[arr[i] for arr in arrays] for i in range(3)])
>> [['A', 1, 'E'], ['B', 2, 'F'], ['C', 3, 'G']]

Comments

0
arr1 = ["A", "B", "C"]
arr2 = [1, 2, 3]
arr3 = ["E", "F", "G"]
arr4 = [[ *i ] for i in zip(arr1, arr2, arr3)]

Output:

[['A', 1, 'E'], ['B', 2, 'F'], ['C', 3, 'G']]

Comments

0

hi you can use also for loop :

l=[]
for i in range(len(arr_1)):
        l.append([arr_1[i],arr_2[i],arr_3[i]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.