0

I want to merge two arrays in to one array in an alternating sequence (arr1,arr2,arr1,arr2,....). After two hours i got this far by trying different methods like concatenate or append or two interlocked "for loops".

As i want to use this regardless of the format (string or integer arrays) i wanted to use a "for loop". My try gives me the correct order, but has some elements missing as the counter isn´t perfect. So what can i do correct that?

Example:

arr1 = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29]
arr2 = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30]
merged = [None]*2*len(arr1)
v = range(0,len(arr1))
for i in v[::2]:
    merged[i] = arr1[i]
    merged[i+1]=arr2[i]

print(merged)

gives [1, 2, 5, 6, 9, 10, 13, 14, 17, 18, 21, 22, 25, 26, 29, 30, None, None, None, None, None, None, None, None, None, None, None, None, None, None]

1
  • 2
    merged = [x for tpl in zip(arr1, arr2) for x in tpl] Commented Sep 20, 2022 at 11:26

7 Answers 7

1

You can simply merge array by using addition operator for example

result_array=array1+array2

Then you can sort it to get your result

result_array.sort()
Sign up to request clarification or add additional context in comments.

4 Comments

they want to interlace the values
seems like i am so impatient. I need to slow down.
sorting is not really the answer... it may work in this particular case but it's obviously not a reliable way to achieve what's expected
Yes I am aware of that, I need to write another one to make it work but you posted the right one already.
0

This could easily be achieved like this:

result = [];
arr1 = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29]
arr2 = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30]

for i in range(len(arr1)): 
    result.append(arr1[i]) 
    result.append(arr2[i]) 
    
print(result)

Comments

0

To merge them such that the values alternate you can simply zip the two lists and iterating over the zipped object will return a tuple with an item from one list and an item at the same index from the other list (order of passing arrays to zip is important) and you can extend your list by that returned tuple:

arr1 = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]
arr2 = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]

merged = []
for values in zip(arr1, arr2):
    merged.extend(values)

Alternatively you can use a simple list comprehension:

merged = [x for tpl in zip(arr1, arr2) for x in tpl]

Comments

0

Keeping with your solution you need to iterate up to the size of the target list, not the smaller one. You also need to divide i by 2 to fit arr indices

arr1 = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]
arr2 = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]
merged = [None] * 2 * len(arr1)
for i in range(0, len(merged) - 1, 2):
    merged[i] = arr1[i // 2]
    merged[i + 1] = arr2[i // 2]

print(merged)
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]

Comments

0

Above solutions works;

Another way to get desired result is;

arr1 = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29]
arr2 = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30]
merged = []
for i in range(0,len(arr1)):
    merged.append(arr1[i])
    merged.append(arr2[i])
print(merged)

1 Comment

Thanks. I tried a variant from another comment wihich is similar to yous and that works.
0
arr1 = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29]
arr2 = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30]
merged = []
[merged.extend([a1, a2]) for a1, a2 in zip(arr1, arr2)]
print(merged)

Comments

0

You can also use numpy, combination of vstack and flatten

import numpy as np
arr1 = np.array([1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29])
arr2 = np.array([2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30])
result = np.vstack((arr1, arr2)).flatten("F")

print(result)

Result:

[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30]

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.