You are currently viewing Add Elements to an Array in Python

How to add elements to an array in Python? 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. You can add elements to an array in Python by using many ways, for example, using the + operator, append(), insert(), and extend() functions. In this article, I will explain add elements to an array in Python using all these methods with examples.

Advertisements

Related: You can append elements to a Python array.

Below are methods to add elements to an array in Python:

  • Using Lists – 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.
  • Using the array module – The array module is a built-in module in Python that provides a way to represent arrays of a specific data type.
  • Using the NumPy module – 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 Add Elements to an Array

If you are in a hurry, below are some quick examples of how to add elements to an array.


# Quick examples of add elements to an array

# Example 1: Add an element to the list
# Using append() method
technology = ['Spark','Python','Pyspark']
technology.append('Pandas')

# Example 2: Use + operator
numbers = [2, 4, 6]
numbers = numbers + [8]

# Example 3: Using insert() method
technology = ['Spark','Python','Java']
technology.insert(0, 'Pandas')

# Example 4: Use extend() function 
technology = ['Pyspark', 'Python']
technology1 = ['Java', 'spark']
technology.extend(technology1)

# Example 5: Add element to an array 
# Using array module
numbers = array.array('i', [0, 5, 10, 15])
numbers.append(20)

# Example 6: Array module 
# Using insert() method
numbers = array.array('i', [0, 4, 6, 8])
numbers.insert(1, 2)

# Example 7: Use extend() method 
# To array module
arr = array.array('i', [5, 10, 15])
mylist = [20, 25, 30]
arr.extend(mylist)

# Example 8: Ad the two arrays 
# Using the + operator
arr = array.array('i', [10, 20, 30])
arr1 = array.array('i', [40, 50, 60])
result_array = arr + arr1

# Example 9: Add element to numpy array 
# Using numpy.append() method
arr = np.array([0, 2, 4, 6])
app_arr = np.append(arr, 8)

# Example 10: Using numpy.insert() method
arr = np.array([10, 20, 30])
arr1 = np.insert(arr, 0, 5)

2. Add Element to an Array Using Lists

You can add an element to an array using a list in a few different ways. For examples

  • Using append() method
  • Using + operator
  • Using insert() method
  • Using extend() method

2.1 Using append() Method

You can use the list append() function to add an element or list of elements in Python. For example, For example, you create a list technology containing the elements ['Spark',' Python','Pyspark']. You then call the append() method on the list and pass in the element you want to add, in this case, pandas. The append() method adds the element to the end of the list.


# Add an element to the list
technology = ['Spark','Python','Pyspark']
print("Actual List",technology)

# Add 'Java' to the list
technology.append('Pandas')
print("Updated List: ",technology)

# Output:
# Actual List ['Spark', 'Python', 'Pyspark']
# Updated List:  ['Spark', 'Python', 'Pyspark', 'Pandas']

2.2 Using + Operator

The + operator can be used to concatenate two lists, effectively adding the elements from one list to the end of another. For example, you create a list numbers containing the elements [2, 4, 6]. you then concatenate the list with another list containing the element you want to add, in this case [8]. The + operator combines the two lists, effectively adding elements 8 to the end of the original list.


# Use + operator
numbers = [2, 4, 6]
numbers = numbers + [8]
print(numbers)

# Output:
# [2, 4, 6, 8]

2.3 Using insert() Method

The insert() method can be used to add an element to a specific position in the list.

You can use the insert() to add an element at a specific index in the Python list. For example, you can insert 'Pandas' it at the first position on the list. For more examples refer to add element to list by index.


# Using insert() method
technology = ['Spark','Python','Java']

# Insert first position
technology.insert(0, 'Pandas')
print(technology)

# Output:
# ['Pandas', 'Spark', 'Python', 'Java']

2.4 Using extend() Method

The list.extend() adds the elements to the existing list at the end. When you add an iterator by using this method, it actually extends the elements as separate elements and adds them to the list. Note that this modifies the existing list with the new elements and the elements are added at the end of the original list. For more examples refer to append multiple elements to the list.


# Use extend() function 
technology = ['Pyspark', 'Python']
technology1 = ['Java', 'spark']

# Extend list to list
technology.extend(technology1)
print(technology)

# Output:
# ['Pyspark', 'Python', 'Java', 'spark']

3. Add Element to an Array Using Array Module

To add an element to an array using the array module in Python, you can use the append() method of the array object. For example, you first create an array numbers of integers using the 'i' type code. you then use the append() method to add the integer 20 to the end of the array.


import array

# Create an array of integers
numbers = array.array('i', [0, 5, 10, 15])

# Append an integer to the end of the array
numbers.append(20)
print(numbers)

# Output:
# array('i', [0, 5, 10, 15, 20])

You can also use the insert() method to add an element at a specific index of the array. For example, you use the insert() method to add the integer 2 at the index 1 of the array. The existing elements are shifted to the right to make room for the new element.


# Create an array of integers
numbers = array.array('i', [0, 4, 6, 8])

# Array module Using insert() method
numbers.insert(1, 2)
print(numbers)

# Output:
# array('i', [0, 2, 4, 6, 8])

You can use extend() method in Python’s array module is used to add elements to an array by appending all the elements of another iterable (such as a list, tuple, or another array) to the end of the array. It essentially elongates the array with the elements from the iterable passed as an argument.


# Create an array of integers
arr = array.array('i', [5, 10, 15])

# Create a list of integers
mylist = [20, 25, 30]

# Use extend() method to array module
arr.extend(mylist)
print(arr) 

# Output:
# array('i', [5, 10, 15, 20, 25, 30])

Similarly, you can also use + operator to add two arrays in Python’s array module. It combines the elements of both arrays to create a new array. For example, you first create two arrays arr and arr1 of integers using the 'i' type code. you then use the + operator to concatenate the two arrays and create a new array result_array that contains all the elements of both arrays.


# Create two arrays of integers
arr = array.array('i', [10, 20, 30])
arr1 = array.array('i', [40, 50, 60])

# Ad the two arrays 
# Using the + operator
result_array = arr + arr1
print(result_array)

# Output:
# array('i', [10, 20, 30, 40, 50, 60])

4. Add Element to NumPy Array

To add 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. you then use the append() function to add the integer 8 to the end of the array.


import numpy as np

# Create a NumPy array of integers
arr = np.array([0, 2, 4, 6])

# Add element to numpy array 
# Using numpy.append() method
app_arr = np.append(arr, 8)
print("Appended array:\n",app_arr)

# Output:
# Appended array: [0 2 4 6 8]

You can also use the insert() function to add an element at a specific index of the array. For example, you can use the insert() function to add the integer 5 at index 0 of the array.


# Create a NumPy array of integers
arr = np.array([10, 20, 30])

# Using numpy.insert() method
arr1 = np.insert(arr, 0, 5)
print(arr1)

# Output:
# [ 5 10 20 30]

Conclusion

In this article, I have explained how to add elements to an array in Python by using the + operator, append(), insert(), and extend() functions with examples.

Happy Learning !!