How to Add Element to Array in Python
Last Updated :
23 Jul, 2025
Appending elements to an array is a frequent operation and Python provides several ways to do it. Unlike lists, arrays are more compact and are designed for more efficient numerical computation. In this article, we will explore different methods for appending to an array.
Using append() Method
The simplest and most commonly used method to append an element to an array in Python is by using append() method. It’s straightforward and works in-place, meaning it modifies the original array directly.
Python
import array
# Create an array
arr = array.array('i', [1, 2, 3])
# Append an element
arr.append(4)
print(arr)
Outputarray('i', [1, 2, 3, 4])
This is generally the most efficient and simple method for appending one element to an array.
Let's take a look at other methods of appending to an array in python:
Using extend() Method
If you want to append multiple elements (i.e., extend the array with another iterable), the extend() method is your best choice. It appends all the elements from the iterable to the array.
Python
import array
# Create an array
arr = array.array('i', [1, 2, 3])
# Extend the array with multiple elements
arr.extend([4, 5, 6])
print(arr)
Outputarray('i', [1, 2, 3, 4, 5, 6])
This method is perfect for when you need to append multiple elements in a single operation.
Using Array Concatenation (+ Operator)
You can also append to an array by concatenating it with another array or iterable using the + operator. This method creates a new array by combining the original array and the new elements.
Python
import array
# Create an array
arr = array.array('i', [1, 2, 3])
# Concatenate arrays
arr = arr + array.array('i', [4, 5])
print(arr)
Outputarray('i', [1, 2, 3, 4, 5])
While concatenation is simple and effective for combining arrays, it’s less efficient than extend() for appending multiple elements to an existing array.
Using a Loop
If you want to append multiple elements one by one, you can use a loop. Although this is less efficient than other methods, it gives you full control over how the elements are added.
Python
import array
# Create an array
arr = array.array('i', [1, 2, 3])
# Append elements using a loop
for i in range(4, 7):
arr.append(i)
print(arr)
Outputarray('i', [1, 2, 3, 4, 5, 6])
This method is more flexible but slower than using extend() when appending multiple elements.
Using fromlist() Method
The fromlist() method can be used to append elements from a list to an array. It's similar to extend(), but it’s specifically designed for lists and arrays.
Python
import array
# Create an array
arr = array.array('i', [1, 2, 3])
# Append elements from a list using fromlist()
arr.fromlist([4, 5, 6])
print(arr)
Outputarray('i', [1, 2, 3, 4, 5, 6])
This method is great for appending from lists, but for general cases, extend() is often better.
Similar Reads
How to Create String Array in Python ? To create a string array in Python, different methods can be used based on the requirement. A list can store multiple strings easily, NumPy arrays offer more features for large-scale data and the array module provides type-restricted storage. Each method helps in managing collections of text values
2 min read
How to Add Floats to a List in Python Adding floats to a list in Python is simple and can be done in several ways. The easiest way to add a float to a list is by using the append() method. This method adds a single value to the end of the list.Pythona = [1.2, 3.4, 5.6] #Add a float value (7.8) to the end of the list a.append(7.8) print(
2 min read
Append Elements to Empty List in Python In Python, lists are used to store multiple items in one variable. If we have an empty list and we want to add elements to it, we can do that in a few simple ways. The simplest way to add an item in empty list is by using the append() method. This method adds a single element to the end of the list.
2 min read
How to append None to a list? The None keyword in Python represents the absence of a value or a null value, so it is mostly used to initialize a variable or to signify the end of a function or it acts as a placeholder for future data.Let's see how we can append None to a list in Python.Using append() methodThe append() method is
2 min read
Python - Add Values to Dictionary of List A dictionary of lists allows storing grouped values under specific keys. For example, in a = {'x': [10, 20]}, the key 'x' maps to the list [10, 20]. To add values like 30 to this list, we use efficient methods to update the dictionary dynamically. Letâs look at some commonly used methods to efficien
3 min read
Update Each Element in Tuple List - Python The task of updating each element in a tuple list in Python involves adding a specific value to every element within each tuple in a list of tuples. This is commonly needed when adjusting numerical data stored in a structured format like tuples inside lists. For example, given a = [(1, 3, 4), (2, 4,
3 min read