Arrays are one of the most popular data structures in the whole programming language world but sadly Python does not support arrays as pre-installed data management sequences, unlike other languages. However, you can import the module named “array” to use them.
An array is an ordered collection of elements that are of a similar type, this type of collection is called a Homogenous collection in Programming. Like a List, an array is also mutable which means that the items in it can be changed or updated.
One of the good features of arrays is that we can decide the type of elements that are going to be stored in them. Let us have a look at how an array can be created!
Array Syntax To use arrays, we will have to import the module “array” like this: import array as arr
This array module has a function named “array” which we will use to create an array. This function two arguments i.e., the data type of the elements that are going to be stored in the array and the array itself in large parenthesis ‘[ ]’.
After importing the module, an array can be created like this –
Example
Note we used letter ‘i’ to specify the data type of elements in the array. By doing this, you cannot insert an element of different data types, e.g., a float or a string. If you try to do that, the Python interpreter will give you an error.
Example
You cannot store a string in an array in Python. You can, however, change the data type of elements to float by specifying in the function.
Example
Note You can convert a list into an array by passing it as an argument in the array() function.
Example
Common data types that can be specified are written below:
Letter
Character Type
Python Type
Byte Size (Minimum)
i
Signed int
int
2
I
Unsigned int
int
2
b
Signed char
int
1
B
Unsigned char
int
1
I
Signed long
int
4
L
Unsigned long
int
4
d
Double
float
8
f
Float
float
4
q
Signed long long
int
8
Q
Unsigned long long
int
8
h
Signed short
int
2
H
Unsigned short
int
2
We mainly use the float (d) and signed int (i) but some programming problems might require other data types too.
Lists vs Arrays
Many programmers call Python Lists as arrays but that is quite not true. Lists can be treated like arrays, but there are some differences that you should know while picking up what to use while programming:
Unlike an array, you cannot specify the data type of elements in the list.
Also, lists do not need to be declared as they are a part of the Python programming language, on the other hand, an array needs to be declared.
Arrays can store data efficiently, therefore, when storing a large amount of data you should consider using arrays instead of lists.
Lists might use more data in the memory, this is due to the fact that the memory while creating an array is fixed but a list is dynamic which means that it can grow depending upon the size of data and it makes a list more data-hungry than an array.
Note
Lists are more useful than arrays, they can deal with elements with different data types and lists are faster too but while dealing with a large amount of data and memory is an issue then you should go for the more efficient ways which in this case is an array.
The usage of arrays normally is not recommended in Python, there are very specific tasks where an array is necessary like interfacing with code of C language.
Accessing Elements
Elements in Python arrays can be accessed using indices of the elements.
Note While counting the index of a specific element, keep in mind that the index of the first element would be zero and so on similar to lists.
Example
Slicing
A slice is a smaller part of a sequence and the process of accessing a smaller part of the sequence is called Slicing.
A slice of an array in Python can be accessed using the slicing operator ( : ), you can access a range of elements only if the range remains valid in the array otherwise you will get an error.
Example
Here you can see that Python prints element on the first index that is passed to the indexing operator until the n-1 index where n is the second index number passed to the indexing operator.
Negative Indexing
Let’s say you do not know how many elements are there in the array and you want to access the last element of it.
Well, it can be done with the help of negative indexing. For example, -1 is the last element, -2 is the second last element, and so on. Let us have some examples to understand it better.
Example
Reversing an Array
With the help of negative indexing, an array can be reversed without the help of any function. This can be done like this: –
Example
Tips Make a habit of assigning slices to a variable so that you can use them again.
Updating an Array
Elements in an array can be changed, removed, or added similar to Lists, and this property of the sequences in Python is called mutability.
Changing Element Values
To change an element, we just have to assign the index of that element to a different value.
Example
Adding Elements
There are multiple ways to add elements in a list. If you want to add a single element in an array, Python allows you to use the append() method or add multiple elements using the extend() method.
Example
The methods append() and extend() always add elements to the end of the list, if you want to add elements at a specific index of a list, you will have to use the insert() method. Let us understand it better using an example:
Example
Note The first argument in the insert() method is used to specify the position of the element which we are going to insert, the second argument is the value of the element.
Two arrays can also be joined using the ‘+’ operator. This property of sequence to join each other is called concatenation in Programming.
Let us have an example: –
Example
This was an another way to add elements in an array.
Deleting Elements
You can also remove elements from an array in Python using the del keyword. With the help of this keyword, whole array can be deleted from the memory.
If you want to delete the elements, you will have to do that element-wise.
Example
Length of an Array
You can use the len() method to get the length of an array. Length is the number of elements the array has.
Example
There are many uses of the length of a list, a few are shown below.
Looping through an Array
Similar to a list, you can print an array element-wise using the for in loop. An array, similar to a list is a type of storage for elements, it makes sense to loop through each of the elements in it. It can be done in this way.
In the example below, there are two ways to print all of the elements of a list.
Example
Finding an Element
To find an element in an array, you can use the built-in Python function index().
This function will return the index of the element in the sequence.
Example
Note You may ask what if there is more than one element with similar value. Well, in that case, the index() will return the index of the first occurrence of the value of the element.