Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to push an array into another array
In programming, an array is a data structure used to store a collection of homogeneous data elements. Each element is identified by an index value. Python doesn't have a specific array data type, so we use lists as arrays.
Arrays in Python
Here's how we represent a list as an array ?
numbers = [1, 4, 6, 5, 3] print(numbers)
[1, 4, 6, 5, 3]
Python indexing starts from 0, so elements are accessed using index values 0, 1, 2, 3, 4.
Pushing an array into another array means inserting all elements from one array into another array. The elements are typically added at the end of the target array.
Input Output Scenarios
Let's see some examples of pushing arrays ?
# Example 1: Integer arrays
A = [1, 2, 3, 4]
B = [5, 6, 7, 8]
print("Before:", A)
A.extend(B)
print("After:", A)
Before: [1, 2, 3, 4] After: [1, 2, 3, 4, 5, 6, 7, 8]
# Example 2: String arrays
A = ['a', 'b', 'c']
B = ['i', 'j', 'k']
print("Before:", B)
B.extend(A)
print("After:", B)
Before: ['i', 'j', 'k'] After: ['i', 'j', 'k', 'a', 'b', 'c']
Method 1: Using the + Operator
The + operator performs list concatenation, creating a new list ?
# Creating arrays
array1 = [1, 4, 5, 6, 5]
array2 = [3, 5, 7, 2, 5]
print('First Array:', array1)
print('Second Array:', array2)
# Pushing using += operator
array2 += array1
print('array2 after pushing array1:', array2)
First Array: [1, 4, 5, 6, 5] Second Array: [3, 5, 7, 2, 5] array2 after pushing array1: [3, 5, 7, 2, 5, 1, 4, 5, 6, 5]
Method 2: Using append() in a Loop
The append() method adds one element at a time. We can use a loop to append each element ?
Syntax
list_obj.append(item)
Example
# Creating arrays
array1 = [1, 4, 5, 6, 5]
array2 = [3, 5, 7, 2, 5]
print('First Array:', array1)
print('Second Array:', array2)
# Pushing using append in loop
for element in array2:
array1.append(element)
print('array1 after pushing array2:', array1)
First Array: [1, 4, 5, 6, 5] Second Array: [3, 5, 7, 2, 5] array1 after pushing array2: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
Method 3: Using extend() Method
The extend() method adds all elements from an iterable to the end of a list. It modifies the original list and returns None.
Syntax
list1.extend(iterable)
Example with Numbers
# Creating arrays
array1 = [1, 4, 5, 6, 5]
array2 = [3, 5, 7, 2, 5]
print('First Array:', array1)
print('Second Array:', array2)
# Pushing using extend
array1.extend(array2)
print('array1 after pushing array2:', array1)
First Array: [1, 4, 5, 6, 5] Second Array: [3, 5, 7, 2, 5] array1 after pushing array2: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
Example with Strings
# Creating string arrays
A = ['a', 'b', 'c']
B = ['i', 'j', 'k']
print('First Array:', A)
print('Second Array:', B)
# Pushing array A into array B
B.extend(A)
print('B after pushing A:', B)
First Array: ['a', 'b', 'c'] Second Array: ['i', 'j', 'k'] B after pushing A: ['i', 'j', 'k', 'a', 'b', 'c']
Comparison
| Method | Modifies Original? | Performance | Best For |
|---|---|---|---|
+ operator |
Creates new list | Slower (copies data) | When you need a new list |
append() loop |
Yes | Slower (multiple calls) | Learning purposes |
extend() |
Yes | Fastest | Most efficient option |
Conclusion
Use extend() for the most efficient way to push one array into another. Use the + operator when you need to create a new combined array without modifying the originals.
