Python Program to push an array into another array


In programming array is a data structure, which is used to store collection of homogeneous data elements. And each element in the array is identified by an index value. But Python doesn’t have a specific data type to represent arrays. Instead, we can use the List as an array.

Arrays in Python

Here, we are representing List as an array.

 [1, 4, 6, 5, 3]

The indexing in python starts from 0, so that the above array elements are accessed using their respective index values 0, 1, 2, 3, 4.

Pushing an array into another array means, inserting all elements present in array_1 into the array array_2. So that the elements of array_1 will be added at the end of array_2.

Input Output Scenarios

Assume we have two arrays A and B with integer values. And the resultant array will have inserted elements of array B into the array A.

Input arrays:
A = [1, 2, 3, 4]
B = [5, 6, 7, 8]

Output array:
[1, 2, 3, 4, 5, 6, 7, 8]

The array B elements 5, 6, 7, 8 are inserted into the end of the array A. Let’s take another set of arrays.

Input arrays:
A = [‘a’, ‘b’, ‘c’]
B = [‘i’, ‘j’, ‘k’]

Output array:
[‘i’, ‘j’, ‘k’, ‘a’, ‘b’, ‘c’]

Below we will discuss different ways to push an array into another array −

Using the “+” Operator

The + operator between the two lists will perform the list concatenation operation. This is also called as push an array into another array.

Example

The “+” operator can easily add the entire array elements behind the other array and hence it performs the pushing operation.

# creating arrays
array1 = [1, 4, 5, 6, 5]
array2 = [3, 5, 7, 2, 5]

print('First Array: ',array1)
print('Second Array: ',array2)

# pushing an array into another array
array2 += array1
 
# Printing concatenated list
print('array2 after pushing arra1:', array2)

Output

First Array:  [1, 4, 5, 6, 5]
Second Array:  [3, 5, 7, 2, 5]
array2 after pushing arra1: [3, 5, 7, 2, 5, 1, 4, 5, 6, 5]

Using Append Method

By using list.append() method we will push an array into another array. The list.append() method is used to add an element at the end of the list. Following is the syntax –

list_obj.append(item)

Example

We traverse the second array using the for loop and keep appending elements to the first array.

# creating arrays
array1 = [1, 4, 5, 6, 5]
array2 = [3, 5, 7, 2, 5]

print('First Array: ',array1)
print('Second Array: ',array2)

# pushing an array into another array
for ele in array2:
    array1.append(ele)
 
# Printing concatenated list
print('array1 after pushing arra2:', array1)

Output

First Array:  [1, 4, 5, 6, 5]
Second Array:  [3, 5, 7, 2, 5]
array1 after pushing arra2: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]

The second array pushed into the first array.

Using Extend() Method

The list.extend() method is a built-in list function that is used to add all the elements of an iterable (list, tuple, string, etc.) to the end of the list. Following is the syntax of this method.

list1.extend(iterable)

Here, all the elements of iterable are added to the end of list1. It modifies the original list i.e., list1. And it doesn't return anything at the output.

Example

Let us see an example –

# creating arrays
array1 = [1, 4, 5, 6, 5]
array2 = [3, 5, 7, 2, 5]

print('First Array: ',array1)
print('Second Array: ',array2)

# pushing an array into another array
array1.extend(array2)
 
# Printing concatenated list
print('array1 after pushing arra2:', array1)

Output

First Array:  [1, 4, 5, 6, 5]
Second Array:  [3, 5, 7, 2, 5]
array1 after pushing arra2: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]

The list.extend() method successfully added the array2 into the array1.

Example

Let’s take another set of arrays with string data and execute the extend() method to push one array elements into another array.

# creating arrays
A = ['a', 'b', 'c']
B = ['i', 'j', 'k']
print('First Array: ',A)
print('Second Array: ',B)

# pushing an array into another array
B.extend(A)
 
# Printing concatenated list
print('array1 after pushing arra2:', B)

Output

First Array:  ['a', 'b', 'c']
Second Array:  ['i', 'j', 'k']
array1 after pushing arra2: ['i', 'j', 'k', 'a', 'b', 'c']

The array A elements ‘a’, ‘b’, ‘c’ are inserted into the array B. These are the few methods to push an array into another array.

Updated on: 29-May-2023

616 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements