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
3 min read
Python | Adding N to Kth tuple element
Many times, while working with records, we can have a problem in which we need to change the value of tuple elements. This is a common problem while working with tuples. Let's discuss certain ways in which N can be added to Kth element of tuple in list. Method #1 : Using loop Using loops this task c
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. [GFGTABS] Python a = [1.2, 3.4, 5.6] #Add a float value (7.8) to the end of the list a.append
2 min read
How to Add Values to Dictionary in Python
The task of adding values to a dictionary in Python involves inserting new key-value pairs or modifying existing ones. A dictionary stores data in key-value pairs, where each key must be unique. Adding values allows us to expand or update the dictionary's contents, enabling dynamic manipulation of d
3 min read
Python - Add list elements to tuples list
Sometimes, while working with Python tuples, we can have a problem in which we need to add all the elements of a particular list to all tuples of a list. This kind of problem can come in domains such as web development and day-day programming. Let's discuss certain ways in which this task can be don
6 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
How to Add Two Numbers in Python
The task of adding two numbers in Python involves taking two input values and computing their sum using various techniques . For example, if a = 5 and b = 7 then after addition, the result will be 12. Using the "+" Operator+ operator is the simplest and most direct way to add two numbers . It perfor
5 min read
How to Append to String in Python ?
In Python, Strings are immutable datatypes. So, appending to a string is nothing but string concatenation which means adding a string at the end of another string. Let us explore how we can append to a String with a simple example in Python. [GFGTABS] Python s = "Geeks" + "ForGeeks
2 min read
How to Update a Dictionary in Python
This article explores updating dictionaries in Python, where keys of any type map to values, focusing on various methods to modify key-value pairs in this versatile data structure. Update a Dictionary in PythonBelow, are the approaches to Update a Dictionary in Python: Using with Direct assignmentUs
3 min read