In the programming language or computer science, an array is defined as the form of a data structure which consists of or store collection of various types of elements inside it. Each element present in the array is identified by one smaller number of its index in the memory of programming language.
- The array is stored in the memory of programming language in such a way that each element can be computed through its index number by using a mathematical formula on the array.
- An array is the most popular container in many programming languages such as C++, C, JavaScript, Java,etc.
Arrays in Python Programming language:
In Python, arrays are the collection of elements of various data-types that are stored inside the contagious locations of our Python memory. An array is a type of container that can hold only a fixed number of elements according to the size of the array, which we have to define while defining the array itself.
- The basic idea of Array is that we can store multiple elements of the same data-type, i.e., int, string, float etc., inside a single container.
- Array in the Python programming language makes it very easier to calculate the position of each element present in the array stored in the Python memory.
- We can calculate each element’s position (Index) inside a Python array by simply adding an offset value to the base value we get through Python operation.
Note:
- Python doesn’t have any built-in support for the array in programming. In this section, we will discuss how we can use Python lists in the form of an array.
- For using Python array support while programming, we have to import Python libraries support arrays in Python such as NumPy library etc.
Why use Python Arrays?
Using arrays in Python is a very important concept in many tasks, and it is useful in many places. Let’s understand through an example that why we need to use Python arrays:
Suppose we have a list of different items with the same data-type in our Python program (let’s take the example of the subject list), storing the list of subjects in a single variable in Python, something looks like this:
sub1 = “Math”
sub2 = “Science”
sub3 = “History”
If we have to look for a specific variable for a specific subject, we need to go through a loop in our program. If the list of subjects is not 3 but 30 or 300 or 3000 maybe, then it creates a problem for us. We have to look for a solution to this problem. We need to find a solution to this problem so that we can run our program very smoothly and easily.
Handling Arrays in Python
The Arrays present in the Python language are handled by a specific module name Array. The array module is very helpful when we have to manipulate or edit the data present in an array of our program. Following are the two basic and important terms that help us to understand the concept of arrays in Python:
- Element
- Index
Element: Each item that we add to our Python array and stored in Python memory is called an element of an array.
Index: Index represents the location of the elements present in the Python array. Each element in the Python array has a numerical index only. This numerical value or index of the element in the array helps us to identify the position of the element present in our array.
Representation of Arrays in Python:
In Python, we can declare an array in many ways and using different methods. But we need to remember some important points always. Following are some important point we need to consider before declaring an array in a Python program:
- The index of each starting element of every array starts with 0 numerical values.
- In arrays, we are able to access each element present in the array using the index of each element.
- We have to first define the length(size) of our array before inserting elements inside that particular array.
- The capacity of the array is only limited to the number we have defined in our array, and we can insert only that particular number of elements in that array.
Methods in Python Arrays
Python provides us some built-in methods through that we can handle Python arrays in our program. These methods can be useful in many operations performing on Python program
Following are some methods that we can use in manipulation, editing or updating of our Python program:
clear() | This method is used to remove all the elements that are present in an array. |
append() | This build-in method in Python is used for adding an element at the end of the array that we have defined in our Python program. |
sort() | The sort() method is used to sort all the elements systematically, alphabetically or numerically etc.(depends upon the user to user) present in an array. |
extend() | Through this method, we can add an element of a list to the end of the list or array defined in our program. |
remove() | The remove() method is used for removing the first item/ element present in the array with specified value. |
copy() | When we use the copy() method on an array present in our Python program, the Python interpreter will return us a copy of that array. |
reverse() | As the name suggests, this method is used for reversing the order of all the elements present in the array. |
insert() | The insert() method is used for inserting an element at any specified position inside the array we have defined in our Python program. |
count() | We use count() method to know how many elements present in our array. It returns in numbers of how many elements are present in our array. |
index() | This function is used to return the index of the element present in our array. When more than one element has the same value, then it returns the index of the first value is matched in the array. |
clear() | When we want to remove all the elements from the array and want to clear the list, we have to use the clear() function. |
Operation in Python Arrays
We can perform many operations on arrays we have defined in our Python program. These operations are used for performing and executing much functionality in our Python program. Operations are very important for Python arrays.
Following are the operations that are supported by the array we have defined in our Python programs:
1. Transverse
2. Deletion
3. Insertion
4. Update
5. Search
Let’s have a look at the brief introduction of above mentioned operations.
Transverse operation on Python Arrays
When we want to print all the elements present inside an array, then we will use this transverse operation on arrays. Transverse operation prints all the elements of our Python array one by one.
Deletion operation on Python Arrays
As the name suggests, when we have to delete all or single elements of the Python array, we will use deletion operation on arrays we have defined in our Python program. The deletion operation deletes the elements of the array from the given index.
Insertion operation on Python Arrays
When we need to add elements in our Python arrays (respect to its size), we perform an insertion operation on our Python arrays. The insertion operation adds element or elements inside arrays defined in our Python program with the given index.
Update operation on Python Arrays
When we want to update an element in our Python array, we have to use an update operation on it. The update operation updates the elements with the given index in our Python arrays.
Search Operation on Python Arrays
When we need to search a specific element in an array defined in our Python program, we will use search operation on it. The search operation is very helpful in searching elements from Python arrays using the index or value of the element.
Defining an Array in our Python program:
We can access the arrays in our Python program by importing the array module in our Python file. The importing of the array module in our Python program is very important because we cannot use array function without importing it.
Following is the syntax of importing array module in the Python program:
1
2
3
|
from array import*
|
Now, when we imported an array module, it’s the time to define an array. The following syntax should be followed for defining an array:
1
2
3
4
|
from array import*
NameOfArray = array(typecode, [Initializers of elements])
|
Through this syntax, we can define arrays in our Python program and can access them.
Accessing elements from an array defined in the Python program:
We can access the elements present in a Python array by using the index of the element we want to access. In Python, we can add a negative (-) sign so that we can access elements from the last indexes.
Example: Let’s see an example where we define an array in our Python program and understand how we can access elements of an array using the indices of the elements we have defined in our Python array.
1
2
3
4
5
6
7
8
|
import array as arr
a = arr.array(‘i’, [2, 4, 6, 8, 10, 12])
print(“The First element of given array is:”, a[0])
print(“The Second element of given array is:”, a[1])
print(“The third last element of given array:”, a[–2])
print(“The second last element of given array:”, a[–1])
|
Output:
1
2
3
4
5
6
|
The First element of given array is: 2
The Second element of given array is: 4
The third last element of given array: 10
The second last element of given array: 12
|
Explanation
In the above example, we have imported an array module so we can define an array in our Python program. After that, we have defined an ‘a’ array with some elements inside it. Then we used the indices of the element to access them and printed them in our program.
We have also used negative indices to access the last elements from the array. This negative index concept is very useful when we have to access elements from last from an array storing many elements in it.
Changing Elements in the Array
The arrays we define in our Python program are mutable in nature. It means we can change or add the elements inside our Python arrays. The elements inside Python arrays can be changed in a very similar way as we make changes in Python lists.
Let’s see an example here for understanding the mutability of arrays in Python:
Example:
1
2
3
4
5
6
7
8
9
10
|
import array as arr
x = arr.array(‘i’, [1, 2, 4, 8, 16, 32])
# changing first element of the array x
x[0] = 0
print(x)
# changing 2nd to 4th element in the array x
x[1:4] = arr.array(‘i’, [64, 128, 256])
print(x)
|
Output:
1
2
3
4
|
array(‘i’, [0, 2, 4, 8, 16, 32])
array(‘i’, [0, 64, 128, 256, 16, 32])
|
Explanation
In the above code, we have defined an array x in our Python program and we have inserted some elements inside the array x. Then we modified element 1st and then element 2nd to 4th in array x respectively with new elements. After modifying the elements inside array x, we printed the output.
Deleting elements from the Python array
When we want to delete an element from the array program, we need to use Python’s del statement with respect to the element index which we want to delete. Suppose we want to delete any particular element from the array. In that case, we just have to define the index of the element inside the Python’s del statement, and Python will delete the element with that particular index from Python memory and array.
Consider the following code for understanding how to delete elements from Python array:
Example:
1
2
3
4
5
6
7
8
9
10
|
import array as arr
x = arr.array(‘i’, [1, 2, 4, 8, 16, 32, 64])
# Removing the second element from array x
del x[1]
print(x)
# Now removing the last element from array x
del x[–1]
print(x)
|
Output:
1
2
3
4
|
array(‘i’, [1, 4, 8, 16, 32, 64])
array(‘i’, [1, 4, 8, 16, 32])
|
Explanation: In the above code, we have defined an array x in our Python program. Then we have inserted some elements inside array x. Then we used Python’s del statement to delete the elements present in array x. We first deleted the 2nd element and then the last element by using the index of the element. After that, we printed the array x as output.
Finding the length of an array in a Python program:
Declaring an array is different in Python because we don’t have to set the array’s length before defining it like in other programming languages. In Python, we can say that the total number of elements present inside a particular array is defined as the length of that array. Sometimes there are many elements present inside an array, so it becomes very hard to count the total number of elements present in that array.
In such cases, when there are many elements present inside a Python array, counting the number of total elements is not a solution. We have to use a pre-defined Python syntax for finding the length of the array.
Following is the syntax which we can find the length of the array defined in our Python program:
len(name_of_our_Python_array)
This syntax will return us an integer value.
The integer value returned by the length syntax is equal to the total number of elements present inside that array.
- Note: Length of Python array = Total number of elements present in that array
Concatenation of Arrays in Python:
Like the other operations, we can also perform concatenation operation on Python arrays. By concatenation operation, we can add two and more arrays together and merge them into a third array or into the first or second array. Like Python lists, we have to use the ‘+’ symbol to concatenate two or more arrays into one inside our Python program.
- Note: If we write c = a+b, then the c array first stores elements of a; after that, it will add the elements of b.
Example: Look at the following code for concatenation of two arrays into a single array:
1
2
3
4
5
6
7
8
|
import array as arr
x=arr.array(‘d’,[1.2 , 2.3 ,3.4,4.5,5.6])
y=arr.array(‘d’,[3.2,5.4])
z=arr.array(‘d’)
z=x+y
print(“Element of array z = “,z)
|
Output:
1
2
3
|
Element of array z = array(‘d’, [1.2, 2.3, 3.4, 4.5, 5.6, 3.2, 5.4])
|
Explanation
In the above code, we have defined two arrays x and y and inserted some elements inside them. Then we have defined a ‘z’ array. After that we used the concatenation operation i.e., z = x+y and printed the output.