Python Program to Merge two Arrays

In this tutorial, we will learn to write Python Python Program to Merge two Arrays. An array is a fundamental data structure in programming that allows us to store multiple elements in a single variable having the same data types.

For Example:

We have two arrays given below

arr1 = [1, 2, 3]
arr2 = [4, 5, 6]

After Merging array will become new_array = [1, 2, 3, 4, 5, 6]

There are many ways to merge two arrays in Python. In this tutorial, we will learn some of them.

Program 1: Using the + operator

With the help of the + operator, We can merge two arrays. The + operator concatenates the elements of two arrays into a single array. Below is the program.

arr1 = [1, 2, 3]
arr2 = [4, 5, 6]
merged_arr = arr1 + arr2
print("Array After Merging =",merged_arr)

Output:

Array After Merging = [1, 2, 3, 4, 5, 6]

Program 2: Using the append() method and a for loop

With the help of append() method and a for loop we can also concatenate two arrays in Python. We will iterate over the elements of the second array and append each element to the end of the first array. Below is a program.

arr1 = [1, 2, 3]
arr2 = [4, 5, 6]
for element in arr2:
    arr1.append(element)
print("Array After Merging =",arr1)

Output:

Array After Merging = [1, 2, 3, 4, 5, 6]

Program 3: Using the extend() method and a for loop

Using extend() method we can also concatenate the two arrays. extend() method adds the elements of one array to the end of another array. Below is the program

arr1 = [1, 2, 3]
arr2 = [4, 5, 6]
arr1.extend(arr2)
print("Array After Merging =",arr1)

Output:

Array After Merging = [1, 2, 3, 4, 5, 6]

Conclusion:

In this post, we have learned to write various approaches to merge two arrays in Python. We have seen the merging of two arrays using the + operator, the extend() method, and the append() method. Hope this tutorial helped you in understanding Python Programs.