Adding Elements to Python Arrays
What will you learn in this guide?
In this guide, you will see different ways to add elements to the array structure in Python.
Although Python does not have a built-in array type, you can create single- or multi-dimensional arrays with the array module and the NumPy library:
- Adding a single element (
append()) - Adding multiple elements (
extend()) - You will be able to perform the adding to a specific location (
insert()) step by step process.
1. Python Array Concept
Python's built-in List structure is flexible, but for large numeric data array or NumPy is more efficient.
arrayModule: Simple and fast for single type (int, float) data.- NumPy Module: Optimized for scientific operations, matrix calculations and multidimensional arrays.
💡 Note: Only elements of the same data type can be added to arrays.
2. Adding Elements with the array Module
The array module has three primary methods that directly modify the array.
a. Combining Two Strings (+ Operator)
import array
arr1 = array.array('i', [10, 20, 30])
arr2 = array.array('i', [40, 50, 60])
arr3 = arr1 + arr2
print("Birleşmiş dizi:", arr3)
This example concatenates two arrays with the + operator.
Output:
array('i', [10, 20, 30, 40, 50, 60])
b. Adding a Single Element to the End – append()
import array
arr1 = array.array('i', [1, 2, 3])
arr1.append(4)
print(arr1)
append() adds a single element to the end of the array.
Output:
array('i', [1, 2, 3, 4])
c. Adding Multiple Elements to the End – extend()
import array
arr1 = array.array('i', [1, 2, 3])
arr1.extend([4, 5, 6])
print(arr1)
extend() appends multiple elements to the end of the array.
Output:
array('i', [1, 2, 3, 4, 5, 6])
d. Insertion to Specific Index – insert(i, x)
import array
arr1 = array.array('i', [1, 2, 3])
arr1.insert(0, 10)
print(arr1)
insert() inserts an element at the specified position.
Output:
array('i', [10, 1, 2, 3])
💡 append() vs extend() Difference
arr = [1, 2, 3]
arr.append([4, 5])
# [1, 2, 3, [4, 5]]
arr.extend([4, 5])
# [1, 2, 3, 4, 5]
append() appends a single object (takes the list as a single element). extend() adds all the elements of the list one by one.
3. Adding Elements to NumPy Arrays
NumPy arrays are fixed size for performance. Therefore, append() or insert() operations do not change the original array, they return a new array.
If NumPy is not installed:
pip install numpy
a. numpy.append() code
import numpy as np
np_arr1 = np.array([[1, 2], [3, 4]])
np_arr2 = np.array([[10, 20], [30, 40]])
# Axis belirtilmezse düzleştirir
flat = np.append(np_arr1, np_arr2, axis=None)
# Satır ekleme
rows = np.append(np_arr1, np_arr2, axis=0)
# Sütun ekleme
cols = np.append(np_arr1, np_arr2, axis=1)
append() adds row (axis=0) or column (axis=1) based.
b. numpy.insert() code
import numpy as np
np_arr1 = np.array([[1, 2], [4, 5]])
np_arr2 = np.array([[10, 20], [30, 40]])
# Satır ekleme
insert_row = np.insert(np_arr1, 1, np_arr2, axis=0)
# Sütun ekleme
insert_col = np.insert(np_arr1, 1, np_arr2, axis=1)
insert() inserts on the specified axis.
4. Performance Summary
| Method | Description | Average Complexity | Note |
|---|---|---|---|
append() | Adds a single element. | O(1) | Fastest method |
extend() | Adds multiple elements. | O(k) | k: number of added elements |
insert() | Adds to specific location. | O(n) | n: array length |
numpy.append() | Returns a new array. | O(n) | Performs light copying |
numpy.insert() | Adds according to axis. | O(n) | Copying costs |
🧠 Big-O Description:
- O(1): Fixed time operation.
- O(n): As the array size increases, the processing time increases.
Frequently Asked Questions (FAQ)
What is the difference between array and list? list can hold different types of data, whereas array holds only one type of numeric data and uses memory more efficiently.
Why doesn't NumPy replace the original array? Because NumPy arrays are fixed size. When you add it, it creates a new copy.
What is the difference between append() and extend()? append() appends an object, extend() appends its contents one by one.
Which method is faster? Appending to the end with append() or extend() is fastest. Appending with insert() is slowest.
🎯 Result
Array manipulation in Python depends on the structure used. Knowing performance differences, data type constraints, and method behavior is critical for both speed and error management.
Accelerate your data analysis and scientific calculations on the GenixNode platform. Instantly test NumPy operations with high-performance server instances 🚀

