Python array is a mutable data structure where values of different data types can be stored. A Python array can store as many values as you want that you can access with the help of a reference number.
They are more like lists, however, the arrays are great for mathematical operations than lists. Also, the lists are built into Python, hence need not be declared. On the other hand, one has to import NumPy to access arrays.
Table of Contents
More about Python Arrays
When it comes to storing data in Python, arrays come into the picture. The values in the arrays are indexed, mutable, repetitive, and can be of any data type.
Arrays are typically used when many related values or a record of details pertaining to a person or object need to be stored. For example, employee details, a student’s record, dimensions of a piece of furniture, etc.
So, let’s get started on the various functions used to perform operations on arrays, such as adding elements, sorting, retrieving elements, and removing, etc.
Declaring arrays in Python
import numpy as np
student_record = ['Joe', 'M', 13, 160.3, 100]
print(student_record)
output:
['Joe', 'M', 13, 160.3, 100]
Yay! We have created an array!
Calling elements in a Python array
In Python, the arrays are indexed and negatively indexed. Meaning?
Look at the tables below:
‘Joe’ | ‘M’ | 13 | 160.3 | 100 |
0 | 1 | 2 | 3 | 4 |
‘Joe’ | ‘M’ | 13 | 160.3 | 100 |
-5 | -4 | -3 | -2 | -1 |
The first table shows the regular indexing in Python arrays, while the 2nd table represents the negative indexing.
When calling a particular value in Python, we use the array index.
For example:
print("The height of the student " + student_record[0] + " is ",student_record[-2], "cm")
output:
The height of the student Joe is 160.3 cm
In the above example, we are using the regular index to access the first element of the array, while the negative index to access the 2nd element from the last.
The negative indexing can come in handy when the user need not find the length of the array to access the last element like in the other programming languages.
Slicing of Python arrays
Retrieving multiple values from the arrays in Python is possible using the array index. We can access the middle elements from the arrays just as we can retrieve a few elements from the rear end of the array.
Here’s an example:
print(student_record[0:3])
output:
['Joe', 'M', 13]
In the above example, we could retrieve the first 3 elements from the array. The elements with 0, 1, 2 indexes. This means, the first index is included but not the last index.
The same output can be achieved using this syntax as well:
print(student_record[:3])
output:
['Joe', 'M', 13]
It means that our syntax has retrieved all the elements until index 2 from the array.
Modifying elements in Python arrays
We can easily modify the elements in the Python array by simply assigning a new value.
For example:
student_record[0] = 'Joseph' print(student_record)
output:
['Joseph', 'M', 13, 160.3, 100]
This means, the value Joe is replaced by Joseph.
Arrays in Python – append() and pop() functions
The append() function comes in handy when we want to add a new value to the array. We can easily add a new element at the end of the array.
For example, in the student record, we hope to add the grade at the end.
student_record.append('8B')
print(student_record)
output:
['Joseph', 'M', 13, 160.3, 100, '8B']
The pop() function performs just the opposite of append(). It removes the element from the array based on the index value you pass as the argument.
Syntax:
student_record.pop(-2)
print(student_record)
output:
['Joseph', 'M', 13, 160.3, '8B']
Python 2D Array
Python allows the concept of 2 d arrays where arrays within the arrays can be used. The 2d arrays use 2 indices to access each element is the array. Consider the 2d array as the one with rows and columns.
Let’s take the example of multiple student records. We can assign 3 records of students and their details in a 2d array.
For example:
student_records = [['Joe', 'M', 13, 160.3, 100],['Emma','F',13,157.5,80],['Chris','M',13,161,102]]
print(student_records)
The output of the 2d array is as follows:
['Joe', 'M', 13, 160.3, 100], ['Emma', 'F', 13, 157.5, 80], ['Chris', 'M', 13, 161, 102]]
Accessing values in Python 2d array
Accessing the data in a 2d array can be done using two indices. One index refers to the parent array and the second index refers to the element in the array within.
For example, syntax:
student_records[1][0]
This generates the output:
'Emma'
Here, 1 is the index of the 2nd sub-array and 0 is the index of the first element in the 2nd sub-array.
To print all the elements in the array, you can use a for loop.
for sub in student_records:
for i in sub:
print(i, end = " ")
print()
The above code generates the output as follows:
Joe M 13 160.3 100 Emma F 13 157.5 80 Chris M 13 161 102
Updating data in Python 2d array
We can update a particular element in the sub array or the entire sub array.
Syntax:
student_records[0][4] = 105
student_records[2] = ['Ginny','F',12,145.2,85]
for sub in student_records:
for i in sub:
print(i, end = " ")
print()
The above code will update a value of 105 in the 1st inner array in the 5th position.
Also, the 3rd inner array is updated with new data ‘Ginny’,’F’,12,145.2,85.
output:
Joe M 13 160.3 105 Emma F 13 157.5 80 Ginny F 12 145.2 85
Inserting data in Python 2d array
We can insert a particular element in the sub array or the entire sub array.
Syntax:
student_records.insert(2, ['Guy','M',13,159.8,101])
for sub in student_records:
for i in sub:
print(i, end = " ")
print()
output:
Joe M 13 160.3 105 Emma F 13 157.5 80 Guy M 13 159.8 101 Ginny F 12 145.2 85
Deleting elements in Python 2d array
We can remove an element from the sub-array or remove the entire inner array using del() using the index. However, to reassign a value to replace the existing one, updating data using the above method can help.
Syntax:
del student_records[2]
for sub in student_records:
for i in sub:
print(i, end = " ")
print()
output:
Joe M 13 160.3 105 Emma F 13 157.5 80 Ginny F 12 145.2 85
Different Python Array methods
Apart from append() and pop(), other functions are quite useful while handling arrays in Python are:
Method | Function |
append() | Adds a new value to the array |
clear() | Removes all the elements from the array |
copy() | Makes a copy of the array |
count() | Returns the number of elements in the array |
index() | Returns the index of the specific value |
insert() | Insert a value in the array at the specified position |
len() | Takes the array name as an argument and returns the Python array length. |
pop() | Removes the element from the array for the specified index |
reverse() | Reverses the order of the array |
sort() | Sorts the elements in the array |
Wrapping it up
Python arrays are flexible data structures that are ideal to perform mathematical operations. There are other mutable and immutable data structures in Pythons like list, string, but Python arrays are well-known for the flexibility they offer.
The list of items in Python arrays can be accessed through loops, however, with the help of the methods like index() and negative indexing, one needn’t use loops for arrays.
On the other hand, the loops are used to access or print all the elements/data in the arrays.