How do I merge into two array, but toggle them. (python)

Asked

Viewed 46 times

0

array0 = [1,2,3] array1 = ['a','b','c']

having the two arrays above, as return this value that is below?

//[1,'a',2,'b',3,'c']

  • Although the title of the question suggested above as duplicate is different, the reply that you have there serves for your case also

1 answer

0


One way is to initialize the resulting array as empty and append each element of the initial arrays alternately.

array0 = [1,2,3]
array1 = ['a','b','c']
array2 = []
for i in range(3):
    array2.append(array0[i])
    array2.append(array1[i])
print(array2)

Note that this solution does not treat arrays of different sizes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.