Array in Python | Set 1 (Introduction and Functions)
Other than some generic containers like lists, Python in its definition can also handle containers with specified data types. The array can be handled in python by a module named “array“. They can be useful when we have to manipulate only specific data type values.
Properties of Arrays
● Each element of the array is of the same data type and same 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 :
1. array(data type, value list):- This function is used to create 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 |
2. append():- This function is used to add the value mentioned in its arguments at the end of the array.
3. insert(i,x) :- This function is used to add the value(x) at the ith position specified in its argument.
Python3
# Python code to demonstrate the working of # array(), append(), insert() # importing "array" for array operations import array # initializing array with array values # initializes array with signed integers arr = array.array( 'i' , [ 1 , 2 , 3 ]) # printing original array print ( "The new created array is : " ,end = " " ) for i in range ( 0 , 3 ): print (arr[i], end = " " ) print ( "\r" ) # using append() to insert new value at end arr.append( 4 ); # printing appended array print ( "The appended array is : " , end = "") for i in range ( 0 , 4 ): print (arr[i], end = " " ) # using insert() to insert value at specific position # inserts 5 at 2nd position arr.insert( 2 , 5 ) print ( "\r" ) # printing array after insertion print ( "The array after insertion is : " , end = "") for i in range ( 0 , 5 ): print (arr[i], end = " " ) |
The new created array is : 1 2 3 The appended array is : 1 2 3 4 The array after insertion is : 1 2 5 3 4
Output:
The newly created array is: 1 2 3 The appended array is: 1 2 3 4 The array after insertion is: 1 2 5 3 4
4. pop():- This function removes the element at the position mentioned in its argument and returns it.
5. remove():- This function is used to remove the first occurrence of the value mentioned in its arguments.
Python3
# Python code to demonstrate the working of # pop() and remove() # importing "array" for array operations import array # initializing array with array values # initializes array with signed integers arr = array.array( 'i' ,[ 1 , 2 , 3 , 1 , 5 ]) # printing original array print ( "The new created array is : " ,end = "") for i in range ( 0 , 5 ): print (arr[i],end = " " ) print ( "\r" ) # using pop() to remove element at 2nd position print ( "The popped element is : " ,end = "") print (arr.pop( 2 )); # printing array after popping print ( "The array after popping is : " ,end = "") for i in range ( 0 , 4 ): print (arr[i],end = " " ) print ( "\r" ) # using remove() to remove 1st occurrence of 1 arr.remove( 1 ) # printing array after removing print ( "The array after removing is : " ,end = "") for i in range ( 0 , 3 ): print (arr[i],end = " " ) |
The new created array is : 1 2 3 1 5 The popped element is : 3 The array after popping is : 1 2 1 5 The array after removing is : 2 1 5
Output:
The newly created array is: 1 2 3 1 5 The popped element is: 3 The array after popping is: 1 2 1 5 The array after removing is: 2 1 5
6. index():- This function returns the index of the first occurrence of the value mentioned in the arguments.
7. reverse():- This function reverses the array.
Python3
# Python code to demonstrate the working of # index() and reverse() # importing "array" for array operations import array # initializing array with array values # initializes array with signed integers arr = array.array( 'i' ,[ 1 , 2 , 3 , 1 , 2 , 5 ]) # printing original array print ( "The new created array is : " ,end = "") for i in range ( 0 , 6 ): print (arr[i],end = " " ) print ( "\r" ) # using index() to print index of 1st occurrence of 2 print ( "The index of 1st occurrence of 2 is : " ,end = "") print (arr.index( 2 )) #using reverse() to reverse the array arr.reverse() # printing array after reversing print ( "The array after reversing is : " ,end = "") for i in range ( 0 , 6 ): print (arr[i],end = " " ) |
The new created array is : 1 2 3 1 2 5 The index of 1st occurrence of 2 is : 1 The array after reversing is : 5 2 1 3 2 1
Output:
The newly created array is: 1 2 3 1 2 5 The index of 1st occurrence of 2 is: 1 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.
Reference :
https://docs.python.org/3/library/array.html#module-array
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Please Login to comment...