Home > Article > Backend Development > Python program to push an array into another array
In programming, an array is a data structure used to store a group of data elements of the same type. Each element in the array is identified by an index value. But Python has no specific data type to represent arrays. Instead, we can use lists as arrays.
Here, we represent List as an array.
[1, 4, 6, 5, 3]
In Python, indexing starts from 0, so the above array elements can be accessed using their respective index values 0, 1, 2, 3, 4.
Pushing an array into another array means inserting all the elements in array array_1 into array array_2. Therefore, the elements of array array_1 will be added to the end of array array_2.
Suppose we have two arrays A and B with integer values. The resulting array inserts the elements of array B into array A.
Input arrays: A = [1, 2, 3, 4] B = [5, 6, 7, 8] Output array: [1, 2, 3, 4, 5, 6, 7, 8]
Elements 5, 6, 7, and 8 of array B are inserted into the end of array A. Let's look at 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 one array into another array -
Using the operator between two lists will perform a list concatenation operation. This is also known as pushing one array into another array.
The " " operator can easily perform a push operation by appending an entire array element to the back of another 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 array2 += array1 # Printing concatenated list print('array2 after pushing arra1:', array2)
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]
By using the list.append() method, we can push an array into another array. The list.append() method is used to add an element to the end of the list. The following is the syntax:
list_obj.append(item)
We use a for loop to iterate through the second array and continue 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)
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]
Push the second array into the first array.
The list.extend() method is a built-in list function used to add all elements of an iterable object (list, tuple, string, etc.) to the end of the list. The following is the syntax of this method.
list1.extend(iterable)
Here, all elements of iterable are added to the end of list1. It modifies the original list, which is list1. and it returns no output.
Let’s look at 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)
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 adds array2 to array1.
Let us take another set of arrays containing string data and execute the extend() method to push the elements of one array 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)
First Array: ['a', 'b', 'c'] Second Array: ['i', 'j', 'k'] array1 after pushing arra2: ['i', 'j', 'k', 'a', 'b', 'c']
The elements 'a', 'b', 'c' of array A are inserted into array B. Here are a few ways to push one array into another.
The above is the detailed content of Python program to push an array into another array. For more information, please follow other related articles on the PHP Chinese website!