An array is a data structure in Python that can store a fixed number of values of the same data type. The basic difference between Python lists and arrays is that lists can store elements of different data types while arrays have a fixed data type.
Arrays are not that common in Python programming. But they are used in cases when memory efficiency is required with lists having elements of the same data type. Arrays are supported by importing the array module in Python. We will learn about creation, indexing, and performing different basic operations on Python arrays.
Creating Python Arrays
The general code for creating arrays in Python is as follows:
from array import * arrayName = array(typeCode, [initializers])
The first line imports the arrays module as we mentioned and the second line declares an array named arrayName using the array method which has two arguments; a type code that specifies the type of the array and optional initializers specifying the elements of the array if any. Following are the most useful type codes available in Python:
Let’s create a simple array of numbers (signed int type):
from array import * numberArray = array('i', [2, 4, 6, 8, 10, 12]) print(numberArray)
Simple printing the array will print its declaration as below:
array('i', [2, 4, 6, 8, 10, 12])
If you want to print only the elements of the array, you can use a for loop:
for i in numberArray: print(i)
The output will be:
2 4 6 8 10 12
Indexing and Slicing Python Arrays
To access the elements of an array, indexing is done using square brackets . Considering the above array, we can print its different elements as:
print("First element: ", numberArray) print("Second element: ", numberArray) print("Last element: ", numberArray[-1])
First element: 2 Second element: 4 Last element: 12
Some important things to note here:
- Array indices start from 0 (not 1)
- The last element can be accessed by providing index ‘-1’ and second last element by ‘-2’ and so on.
If we want to access a range of elements of an array we use the slicing operator ‘:’ with the square brackets. Specify the starting index (inclusive) before the semi-colon and the ending index (exclusive) after.
print(numberArray[1:4]) #2nd to 4th element print(numberArray[:-4]) #beginning to 3rd print(numberArray[1:]) #2nd to end print(numberArray[:]) #the whole array
array('i', [4, 6, 8]) array('i', [2, 4, 6]) array('i', [4, 6, 8, 10, 12]) array('i', [2, 4, 6, 8, 10, 12])
Adding elements to Python Arrays
We can add elements to arrays using insert method which takes the index and the element as the arguments.
numberArray.insert(1, 2) print(numberArray)
array('i', [2, 2, 4, 6, 8, 10, 12])
Updating elements in Python Arrays
Elements of an array can be updated just by assigning new values to the same index using the assignment operator in Python ‘=’:
numberArray = 0 print(numberArray)
array('i', [0, 2, 4, 6, 8, 10, 12])
Deleting elements in Python Arrays
del keyword of Python can be used to delete an element of an array by specifying its index:
del numberArray print(numberArray) del numberArray #delete the whole array
array('i', [0, 2, 4, 8, 10, 12])
Common Methods for Python Arrays
Following are some other useful methods that can by applied to Python Arrays:
- remove() – to delete elements of the specified element
- pop() – to remove and return the first element of an array
- append() – to add one item to the array at the end
- extend() – to add an array at the end of another one
- index() – to find the index of the specified element
This is all about the basics of one-directional arrays in Python. Feel free to comment below in case of any queries.