// Tutorial //

Python add to Array

Published on August 3, 2022
Default avatar
By Pankaj
Developer and author at DigitalOcean.
Python add to Array

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Python doesn’t have any specific data type as an array. We can use List that has all the characteristics of an array. Python array module can be used to create an array of integers and floating-point numbers. If you want to do some mathematical operations on an array, you should use the NumPy module.

1. Python add to Array

  • If you are using List as an array, you can use its append(), insert(), and extend() functions. You can read more about it at Python add to List.
  • If you are using array module, you can use the concatenation using the + operator, append(), insert(), and extend() functions to add elements to the array.
  • If you are using NumPy arrays, use the append() and insert() function.

2. Adding elements to an Array using array module

  • Using + operator: a new array is returned with the elements from both the arrays.
  • append(): adds the element to the end of the array.
  • insert(): inserts the element before the given index of the array.
  • extend(): used to append the given array elements to this array.
import array

arr1 = array.array('i', [1, 2, 3])
arr2 = array.array('i', [4, 5, 6])

print(arr1)  # array('i', [1, 2, 3])
print(arr2)  # array('i', [4, 5, 6])

arr3 = arr1 + arr2
print(arr3)  # array('i', [1, 2, 3, 4, 5, 6])

arr1.append(4)
print(arr1)  # array('i', [1, 2, 3, 4])

arr1.insert(0, 10)
print(arr1)  # array('i', [10, 1, 2, 3, 4])

arr1.extend(arr2)
print(arr1)  # array('i', [10, 1, 2, 3, 4, 4, 5, 6])

3. Adding elements to the NumPy Array

  • append(): the given values are added to the end of the array. If the axis is not provided, then the arrays are flattened before appending.
  • insert(): used to insert values at the given index. We can insert elements based on the axis, otherwise, the elements will be flattened before the insert operation.
>>> import numpy as np
>>> np_arr1 = np.array([[1, 2], [3, 4]])
>>> np_arr2 = np.array([[10, 20], [30, 40]])
>>> 
>>> np.append(np_arr1, np_arr2)
array([ 1,  2,  3,  4, 10, 20, 30, 40])
>>>
>>> np.append(np_arr1, np_arr2, axis=0)
array([[ 1,  2],
       [ 3,  4],
       [10, 20],
       [30, 40]])
>>>
>>> np.append(np_arr1, np_arr2, axis=1)
array([[ 1,  2, 10, 20],
       [ 3,  4, 30, 40]])
>>> 
>>> np.insert(np_arr1, 1, np_arr2, axis=0)
array([[ 1,  2],
       [10, 20],
       [30, 40],
       [ 3,  4]])
>>> 
>>> np.insert(np_arr1, 1, np_arr2, axis=1)
array([[ 1, 10, 30,  2],
       [ 3, 20, 40,  4]])
>>> 

4. References


Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar
Pankaj

author

Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?

all we want to do is something really simple like somarray = [0] * 256 somearray[5] = 100 but it will throw list index out of range, even after it was created with the right size why cant you just write simple solution to this problem

- kop

    How can I insert element at given position by using array module without using any in built function in python

    - Mohanish

      Thank you! very helpful

      - Jul