How to Add Element to Array in Python
Last Updated :
29 Nov, 2024
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
Find element in Array - Python Finding an item in an array in Python can be done using several different methods depending on the situation. Here are a few of the most common ways to find an item in a Python array.Using the in Operatorin operator is one of the most straightforward ways to check if an item exists in an array. It r
3 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
Add Elements of Two Lists in Python Adding corresponding elements of two lists can be useful in various situations such as processing sensor data, combining multiple sets of results, or performing element-wise operations in scientific computing. List Comprehension allows us to perform the addition in one line of code. It provides us a
3 min read
Convert list to Python array - Python We can use a number of approaches to convert a list into a Python array based on the requirements. One option is to use the array module, which allows us to create arrays with a specified data type. Another option is to use the numpy.array() method, which provides more features for working with arra
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