Home > Backend Development > Python Tutorial > Python program to push an array into another array

Python program to push an array into another array

王林
Release: 2023-08-31 15:37:06
forward
1448 people have browsed it

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.

Arrays in Python

Here, we represent List as an array.

 [1, 4, 6, 5, 3]
Copy after login

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.

Input and output scenarios

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]
Copy after login

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’]
Copy after login

Below we will discuss different ways to push one array into another array -

Use " " operator

Using the operator between two lists will perform a list concatenation operation. This is also known as pushing one array into another array.

Example

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)
Copy after login

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]
Copy after login

Use append method

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)
Copy after login

Example

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)
Copy after login

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]
Copy after login
Copy after login

Push the second array into the first array.

Use the Extend() method

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)
Copy after login

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.

Example

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)
Copy after login

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]
Copy after login
Copy after login

The list.extend() method successfully adds array2 to array1.

Example

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)
Copy after login

Output

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template