What is an Array in Python 3?
An array is a data structure that can contain or hold a fixed number of elements that are of the same Python data type. An array is composed of an element and an index. Index in an array is the location where an element resides. All elements have their respective indices. Index of an array always starts with 0.
Watch this Python Pandas Tutorial Video for Beginners:
Unlike other programming languages, such as Java, C, C++, and more, arrays are not that popular in Python since there are many iterable data types in Python that are flexible and fast to use such as Python lists. However, arrays in Python 3 are still used in certain cases. In this module, we will learn all about all the important aspects of arrays in Python 3, from what they are to when they are used.
Following is the list of topics covered in this Python module.
- Difference_between_Array_and_List in Python
- Creating an Array in Python 3
- Accessing a Python Array Element
- Basic Operations of Arrays in Python
So, without further delay, let’s get started.
Learn more about Python from this Python for Data Science Course to get ahead in your career!
Difference between Array and List in Python
The basic difference between arrays and lists in Python is that lists are flexible and can hold completely arbitrary data of any data type while arrays can only hold data of the same data type.
Arrays are considered useful in terms of memory efficiency, but they are usually slower than lists. As mentioned above, the Python array module is not that popular to use, but they do get used in certain cases such as:
Creating an Array in Python 3
The array module supports numeric arrays in Python 3. So, to create an array in Python 3, we will have to import the array module. Following is the syntax for creating an array. Now to understand how to declare array in Python, let us take a
look at the python array example given below:
from array import * arraname = array(typecode, [Initializers])
Here, typecode is what we use to define the type of the value that is going to be stored in the array. Some of the common typecodes used in the creation of arrays in Python are described in the following table.
|Type Code||C Type||Python Data Type||Minimum Size in Bytes|
Now, let’s create a Python array using the above-mentioned syntax.
import array as arr a = arr.array(‘I’, [2,4,6,8]) print(a) Output: array(‘I’, [2, 4, 6, 8])
Interested in learning Python? Enroll in our Python Course in London now!
Accessing a Python Array Element
We can access the elements of an array in Python using the respective indices of those elements, as shown in the following example.
from array import* array_1 = array(‘i’, [1,2,3,4,5]) print (array_1) print (array_1) 1 4
The index of the array elements starts from 0. When we printed the value of array1, it displayed the first element.
Basic Operations of Arrays in Python
Following are some of the basic operations supported by array module in Python:
- Traverse of an Array in Python: Iterating between elements in an array is known as traversing. We can easily iterate through the elements of an array using Python for loop as shown in the example below.
from array import * array_1 = array(‘i’, [1,2,3,4,5]) for x in array_1: print (x) Output: 1 2 3 4 5
- Insertion of Elements in an Array in Python:Using this operation, we can add one or more elements to any given index.
from array import * array_1 = array(‘i’, [1,2,3,4,5]) array_1.insert(1,6) for x in array_1: print (x) Output: 1 6 2 3 4 5
- Deletion of Elements in an Array in Python:Using this operation, we can delete any element residing at a specified index. We can remove any element using the built in remove() method.
from array import * array_1 = array(‘i’, [1,2,3,4,5]) array_1.remove(2) For x in array_1: print (x) Output: 1 3 4 5
- Searching Elements in an Array in Python:Using this operation, we can search for an element by its index or its value.
from array import * array_1 = array(‘i’, [1,2,3,4,5]) print (array_1.index(3)) Output: 2
In the above example, we have searched for the element using the built-in index() method. Using index(3) returned the output 2 which means that 3 is at the index number 2 in array_1. If the searched value is not present in the array, then the program will return an error.
- Updating Elements in an Array in Python:Using this operation, we can update an element at a given index.
from array import * array_1 = array(‘i’, [1,2,3,4,5]) array_1 = 100 for x in array_1: print(x) Output: 1 2 100 4 5
Kick-start your career in Python with the perfect Python Course in New York now!
In the above example, we have updated the already existing value at index 2 instead of adding a new element.
With this, we have come to the end of this module in Python Tutorial. We learnt about arrays in Python 3, how to define array in Python 3, accessing an Python Array, and different operations of Python array. Now, if you are interested in knowing why python is the most preferred language for data science, you can go through this blog on Python for data science