If we're working with arrays in Python, we might often need to add items to an array. In this article, we’ll explore how to add items to an array using different methods.
Add single element - Using append()
The append() method adds a single element to the end of the array. This is one of the most straightforward ways to add an item to an array in Python.
Python
import array
# Create an array
arr = array.array('i', [1, 2, 3])
# Add an item using append()
arr.append(4)
print(arr)
Outputarray('i', [1, 2, 3, 4])
Let's explore other methods of adding item in array :
Add multiple items - Using extend()
If we want to add multiple items to an array at once, you can use the extend() method. This method adds each item from an iterable (like a list or another array) to the array.
Python
import array
# Create an array
arr = array.array('i', [1, 2, 3])
# Add multiple items using extend
arr.extend([4, 5, 6])
print(arr)
Outputarray('i', [1, 2, 3, 4, 5, 6])
Add item at position - Using insert()
The insert() method allows us to add an element at any position in the array. We specify the index where we want to insert the new element.
Python
import array
# Create an array
arr = array.array('i', [1, 2, 4, 5])
# Insert an item at index 2
arr.insert(2, 3)
print(arr)
Outputarray('i', [1, 2, 3, 4, 5])
Using Slicing and Assignment
Another way to add items to an array is by using slicing. We can assign new values to a slice of the array, effectively adding items at any position.
Python
import array
# Create an array
arr = array.array('i', [1, 2, 4, 5])
# Add an item at index 2 using slicing
arr[2:2] = array.array('i', [3]) # Convert the list to an array
print(arr)
Outputarray('i', [1, 2, 3, 4, 5])
Using + Operator (Concatenation)
Although this isn’t the most efficient method for adding a single item, we can concatenate arrays using the + operator. This method is typically used to join two arrays but it can also be used to add a new item.
Python
import array
# Create an array
arr = array.array('i', [1, 2, 3])
# Add an item using concatenation
arr = arr + array.array('i', [4])
print(arr)
Outputarray('i', [1, 2, 3, 4])
Using array() Constructor with + Operator
If we are adding multiple items or even replacing the entire array, we can recreate the array using the array() constructor and the + operator.
Python
import array
# Create two arrays
arr1 = array.array('i', [1, 2, 3])
arr2 = array.array('i', [4, 5, 6])
# Concatenate using the + operator
arr = arr1 + arr2
print(arr)
Outputarray('i', [1, 2, 3, 4, 5, 6])
Similar Reads
Python - Add List Items Python lists are dynamic, which means we can add items to them anytime. In this guide, we'll look at some common ways to add single or multiple items to a list using built-in methods and operators with simple examples:Add a Single Item Using append()append() method adds one item to the end of the li
3 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
Python Unpack Array The unpacking techniques you use with lists also apply to python arrays, but with slight differences due to the nature of the array module. In this article, we'll explore how to unpack arrays using the array module in Python, and demonstrate various methods of unpacking.Basic Array UnpackingTo begin
3 min read
Python - Access List Item Whether we are working with numbers, strings or other data types, lists provide a versatile way to organize and manipulate data. But how to access specific items in a list? This article will guide you through various methods of accessing list items in Python.Accessing List Items by IndexIn Python, l
3 min read
Python Access Set Items Python sets are unordered collections of unique elements. Unlike lists or dictionaries, sets do not have indices so accessing elements in the traditional way using an index doesn't apply. However, there are still ways to work with sets and access their items effectively. This article will guide you
2 min read