Other than some generic containers like lists, Python in its definition can also handle containers with specified data types. The array can be handled inPython by a module named "array". They can be useful when we have to manipulate only specific data type values.
Properties of Arrays
- Each array element is of the same data type and size. For example: For an array of integers with the int data type, each element of the array will occupy 4 bytes.
- Elements of the array are stored in contiguous memory locations.
Operations on Array in Python
Below are some operations that can be performed in an array:
- append()
- insert()
- pop()
- remove()
- index()
- reverse()
array(data type, value list) in Python
This function is used tocreate an array with data type and value list specified in its arguments. Some data types are mentioned in the table below.
Type Code | C Type | Python Type | Minimum size in Bytes |
---|
'b' | signed char | int | 1 |
'B' | unsigned char | int | 1 |
'u' | Py_UNICODE | Unicode character | 2 |
'h' | signed short | int | 2 |
'H' | unsigned short | int | 2 |
'i' | signed int | int | 2 |
'I' | unsigned int | int | 2 |
'l' | signed long | int | 4 |
'L' | unsigned long | int | 4 |
'q' | signed long long | int | 8 |
'Q' | unsigned long long | int | 8 |
'f' | float | float | 4 |
'd' | double | float | 8 |
Python append() Method
This function is used toadd the value mentioned in its arguments at the end of the array. In this example, we are adding an element at the end of the array.
Python3# importing "array" for array operationsimportarray# initializing array with array values and signed integersarr=array.array('i',[1,2,3])# printing original arrayprint("The new created array is : ",end=" ")foriinrange(0,3):print(arr[i],end=" ")print("\r")# using append() to insert new value at endarr.append(4);# printing appended arrayprint("The appended array is : ",end="")foriinrange(len(arr)):print(arr[i],end=" ")
OutputThe new created array is : 1 2 3 The appended array is : 1 2 3 4
Array insert(i,x) Method in Python
This function is used toadd the value(x) at the ith positionspecified in its argument. In this example, we are inserting an element at a specified index in an array.
Python3# importing "array" for array operationsimportarray# initializing array with array values and signed integersarr=array.array('i',[1,2,3])# printing original arrayprint("The new created array is : ",end=" ")foriinrange(0,3):print(arr[i],end=" ")arr.insert(2,5)print("\r")# printing array after insertionprint("The array after insertion is : ",end="")foriinrange(len(arr)):print(arr[i],end=" ")
OutputThe new created array is : 1 2 3 The array after insertion is : 1 2 5 3
Time Complexity: The initialization of the array takes O(n) time, where n is the number of elements in the array. The for loops used for printing the original array and the appended/inserted array takes O(n) time each, where n is the number of elements in the array. The append() method takes O(1) time as it adds the new element at the end of the array in constant time. The insert() method takes O(n) time in the worst case as it may have to shift all the elements to the right of the given position by one index, where n is the number of elements in the array. Therefore, the overall time complexity of the code is O(n) + O(n) + O(1) + O(n) = O(n).
Auxiliary Space: The array data structure used for initialization, append() and insert() methods requires a fixed amount of space for each element in the array. The space used by the print statements is negligible compared to the space used by the array. Therefore, the auxiliary space complexity of the code is O(n), where n is the number of elements in the array.
array pop() Method in Python
This functionremoves the element at the position mentioned in its argument and returns it. In this example, we are removing an element mentioned at a particular index.
Python3# importing "array" for array operationsimportarray# initializing array with array valuesarr=array.array('i',[1,2,3,1,5])# printing original arrayprint("The new created array is : ",end="")foriinrange(0,5):print(arr[i],end=" ")print("\r")# using pop() to remove element at 2nd positionprint("The popped element is : ",end="")print(arr.pop(2));# printing array after poppingprint("The array after popping is : ",end="")foriinrange(len(arr)):print(arr[i],end=" ")
OutputThe new created array is : 1 2 3 1 5 The popped element is : 3The array after popping is : 1 2 1 5
Array remove() in Python
This function is used toremove the first occurrence of the value mentioned in its arguments. In this example, we are removing the first occurrence of 1 from a given array.
Python3# importing "array" for array operationsimportarray# initializing array with array valuesarr=array.array('i',[1,2,3,1,5])# printing original arrayprint("The new created array is : ",end="")foriinrange(0,5):print(arr[i],end=" ")print("\r")# using remove() to remove 1st occurrence of 1arr.remove(1)# printing array after removingprint("The array after removing is : ",end="")foriinrange(len(arr)):print(arr[i],end=" ")
OutputThe new created array is : 1 2 3 1 5 The array after removing is : 2 3 1 5
Array index() Method
This function returns theindex of the first occurrence of the value mentioned in the arguments. In this example, we are finding the index of 1st occurrence of 2 and printing it.
Python3# importing "array" for array operationsimportarray# initializing array with array valuesarr=array.array('i',[1,2,3,1,2,5])# printing original arrayprint("The new created array is : ",end="")foriinrange(0,6):print(arr[i],end=" ")print("\r")# using index() to print index of 1st occurrence of 2print("The index of 1st occurrence of 2 is : ",end="")print(arr.index(2))
OutputThe new created array is : 1 2 3 1 2 5 The index of 1st occurrence of 2 is : 1
Array reverse() Function
This functionreverses the array. In this example, we are reversing the array by using reverse().
Python3# importing "array" for array operationsimportarray# initializing array with array valuesarr=array.array('i',[1,2,3,1,2,5])# printing original arrayprint("The new created array is : ",end="")foriinrange(0,6):print(arr[i],end=" ")print("\r")#using reverse() to reverse the arrayarr.reverse()# printing array after reversingprint("The array after reversing is : ",end="")foriinrange(len(arr)):print(arr[i],end=" ")
OutputThe new created array is : 1 2 3 1 2 5 The array after reversing is : 5 2 1 3 2 1
Where can arrays be used?
- Arrays should be used where the number of elements to be stored is already known.
- Arrays are commonly used in computer programs to organize data so that a related set of values can be easily sorted or searched.
- Generally, when we require very fast access times, we usually prefer arrays since they provide O(1) access times.
- Arrays work well when we have to organize data in multidimensional format. We can declare arrays of as many dimensions as we want.
- If the index of the element to be modified is known beforehand, it can be efficiently modified using arrays due to quick access time and mutability.
Related Article:Array in Python | Set 2 (Important Functions)