Are Arrays Mutable in Python?

Are Arrays Mutable (1)

We can classify Python objects into two major categories i.e mutable and immutable objects. Mutable objects are those which can be changed or modified once they are created whereas immutable objects cannot be altered once they are created. Arrays come under the category of mutable objects. In this article, we will learn about arrays and their mutability, and the operations you can perform on arrays. So let’s get started!

What Are Arrays in Python?

An array is a data structure in Python that stores a collection of similar types of objects. The objects in an array are indexed by a tuple of positive integers. They can be multi-dimensional and very useful for scientific computing.

For example:

import numpy as np
list=[1,2,3,4]
arr = np.array(list)
print(arr)

Output:

[1 2 3 4]

In the above example, we have created a 1-D array from a list.

You can access the array elements by the following method.

import numpy as np
list=[1,2,3,4]
arr = np.array(list)
print("First element of array is:",arr[0]) 
print("Last element of array is:",arr[-1])

Output:

First element of array is: 1
Last element of array is: 4

Now we will look at the mutable properties of an array.

Mutable Properties of Array

Now we will see what all changes we can make in array with examples.

Inserting Elements in an Array

The insert function helps you to insert elements in an array. The function takes up two arguments which are the index position where you want to insert the element and the value of the element.

import array as np
 
a = np.array('i', [1, 2, 3])

#using insert function
a.insert(1, 4)
print(a)

Output:

array('i', [1, 4, 2, 3])

Modifying Elements in an Array

You can modify the elements in an array with the help of the following code. 

import array as np
 
a = np.array('i', [1, 2, 3])

#using insert function
a[1]=9
print(a)

Output:

array('i', [1, 9, 3])

You need to specify the index position of the element you want to modify.

Popping an Element in an Array

The pop() function will help you in popping an element out. You need to specify the index position of the element you want to pop. This function acts similar to the delete operation.

import array as np
 
a = np.array('i', [1, 2, 3])

#using pop function
a.pop(1)
print(a)

Output:

array('i', [1, 3])

Deleting or Removing Elements From an Array

The remove() function will help you to remove elements from an array. You have to specify the value of the element you want to remove.

import array as np
 
a = np.array('i', [1, 2, 3])

#using remove function
a.remove(3)
print(a)

Output:

array('i', [1, 2])

Reversing an Array

The simple reverse() function will help you in reversing an array.

import array as np
 
a = np.array('i', [1, 2, 3])

#using remove function
a.reverse()
print(a)

Output:

array('i', [3, 2, 1])

Conclusion

In summary, we learned that arrays are mutable and can be modified or altered after they are created. It is very crucial that you understand the basic array operations as arrays can be extremely useful in scientific computing.