How to append an element to an array in Python? In Python, you can use the append() method to append an element to the end of an array. However, it’s important to note that Python does not have a built-in array data type, but you can use lists, the array module, or the NumPy module Read More Python, Python Tutorial, Python Array Examples Spark By {Examples}
How to append an element to an array in Python? In Python, you can use the append() method to append an element to the end of an array. However, it’s important to note that Python does not have a built-in array data type, but you can use lists, the array module, or the NumPy module to represent arrays. In this article, I will explain append to an array using the append() method from Python and NumPy with examples.
Below are methods to append to an array in Python:
Python List – A list is a built-in data type in Python that can be used to represent arrays. Lists are very flexible and can contain elements of different data types.
Python Array module – The array module is a built-in module in Python that provides a way to represent arrays of a specific data type.
Python NumPy array – NumPy is a popular third-party library for scientific computing in Python. It provides a powerful N-dimensional array object that can be used to represent arrays.
1. Quick Examples of Append to Array
If you are in a hurry, below are some quick examples of how to append to an array in python.
# Quick examples of append to array
# Example 1: Use append() function with lists
technology = [‘Java’, ‘Spark’, ‘Python’, ‘Pyspark’]
technology.append(‘Hyperion’)
#Example 2: loop to append multiple elements
# Using append() method
mylist = []
for i in range(6):
mylist.append(i)
# Example 3: Use append() method
# with the array module
arr = array.array(‘i’, [0, 2, 4, 6])
arr.append(8)
# Example 4: Use array module to floating-point
arr = array.array(‘d’, [3.7, 5.2, 7.4])
arr1 = 15
arr.append(arr1)
# Example 5: Use append() method
# create an empty array
arr = array.array(‘i’)
arr.append(5)
arr.append(10)
arr.append(15)
# Example 6: Use appemd() method with numpy array
# Using numpy.append() method
arr = np.array([1, 5, 9, 16])
app_arr = np.append(arr, 20)
# Example 7: Use append() method
# with numpy array
arr = np.arange(4)
arr1 = np.arange(6, 10)
result = np.append(arr, arr1)
2. Use append() Function with Lists
You can use the append() function is used to append elements to the end of a list. For example, you have a list of technology with four elements. You use the append() method to add the string ‘Hyperion’ to the end of the list.
2.1 Syntax of list.append() Function
Following is a syntax of the append() function.
# Syntax of list.append() function
list.append(item)
2.2 Parameters of append()
item – The item to be appended to the list. It can be of any data type (e.g. string, integer, list, etc.).
2.3 Return Value
This function does not return any value but updates the existing list.
# Consider a list
technology = [‘Java’, ‘Spark’, ‘Python’, ‘Pyspark’]
print(“Actual List”,technology)
# Use append() function with lists
technology.append(‘Hyperion’)
print(“Updated List: “,technology)
# Output
# Actual List [‘Java’, ‘Spark’, ‘Python’, ‘Pyspark’]
# Updated List: [‘Java’, ‘Spark’, ‘Python’, ‘Pyspark’, ‘Hyperion’]
You can also use a loop to append multiple elements to a list using the append() method. For example, you create an empty list mylist and then use a loop to add the integers 0 through 5 to the list using the append() method.
# loop to append multiple elements
# Using append() method
mylist = []
for i in range(6):
mylist.append(i)
print(mylist)
# Output
# [0, 1, 2, 3, 4, 5]
3. Use the Python append() Method with the Array Module
You can use the array module to provide a array() function that creates an array object, which is similar to a list but more efficient for certain types of data. The array object has its own append() method that you can use to add elements to the end of the array. For example, you first import the array module and then use the array() function to create an integer array arr with four elements. You use the append() method of the arr object to add an integer 8 to the end of the array.
import array
# Create an array of integers
arr = array.array(‘i’, [0, 2, 4, 6])
# Use append() method
# with the array module
arr.append(8)
print(arr)
# Output
# array(‘i’, [0, 2, 4, 6, 8])
# Use array module to floating-point
arr = array.array(‘d’, [3.7, 5.2, 7.4])
arr1 = 15
arr.append(arr1)
print(arr)
# Output
# array(‘d’, [3.7, 5.2, 7.4, 15.0])
You can also create an empty array and then append elements. For example, you create an empty integer array arr and then use the append() method to add three integers to the array.
import array
# Use append() method
# create an empty array
arr = array.array(‘i’)
arr.append(5)
arr.append(10)
arr.append(15)
print(arr)
# Output
# array(‘i’, [5, 10, 15])
4. Use append() Method with NumPy Array
To append elements to a NumPy array in Python, you can use the append() function provided by the NumPy module. For example, you first create a NumPy array arr of integers using the array() function provided by the NumPy module and use the append() function to add the integer 20 to the end of the array.
import numpy as np
# Create a NumPy array of integers
arr = np.array([1, 5, 9, 16])
# Use appemd() method with numpy array
# Using numpy.append() method
app_arr = np.append(arr, 20)
print(“Appended array:n”,app_arr)
# Output
# Appended array: [1 5 9 16 20]
Finally, let’s see how to append two NumPy array variables. numpy.arange() is used to create a NumPy array. The append() method can also be used with NumPy arrays to add new elements to the end of the array. However, it is important to note that append() creates a new array every time it is called, which can be inefficient for large arrays.
# Use append() method with numpy array
arr = np.arange(4)
arr1 = np.arange(6, 10)
result = np.append(arr, arr1)
print(“Appended array : “, result)
# Output
# Appended array : [0 1 2 3 6 7 8 9]
Conclusion
In this article, I have explained how to append to an array in Python by using the append() function with examples.
Happy Learning !!