![]() |
VOOZH | about |
In Python, Arrays are a type of container that can store elements of the same data type more efficiently. They are provided by the built-in array module and are useful when working with large amounts of numeric data where performance and memory efficiency matter.
The array() function from Python's array module creates an array with elements of a specified data type. It is used to store homogeneous data.
array(typecode, [value1, value2, ...])
Parameter:
Some data types are mentioned in 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 arrays support various built-in methods to manipulate and manage their elements efficiently. These operations help in adding, removing, searching or modifying data within the array.
Let’s explore each array method one by one with a simple explanation and example:
append() method adds a specified value to the end of the array.
Example: This code demonstrates how to create an integer array using array module and then append a new value to it using append() method. It first prints original array, adds number 4 at the end and finally displays updated array.
The new created array is : 1 2 3 The appended array is : 1 2 3 4
insert() method is used to add a value at a specific index in an array. It takes two arguments, position where the element should be inserted and the value to insert.
Example: It demonstrates to use insert() method to add an element at a specific position in an array. It starts by creating an array of signed integers, prints original array, then inserts value 5 at index 2.
The new created array is : 1 2 3 The array after insertion is : 1 2 5 3
pop() method is used to remove and return element at specified index in an array. If no index is given, it removes the last element by default.
Example: This example shows how to use pop() method. It first initializes an array with integer values, then removes element at index 2 using pop(2) and prints removed element.
The new created array is : 1 2 3 1 5 The popped element is : 3 The array after popping is : 1 2 1 5
remove() method is used to delete the first occurrence of a specific value from the array.
Example: This code shows how to use remove() method to delete first occurrence of a specified value (in this case, 1).
The new created array is : 1 2 3 1 5 The array after removing is : 2 3 1 5
index() method is used to find the position of the first occurrence of a given value in the array.
Example: This code shows how to use index() method. It first creates an array of integers, prints original array and then finds the index of the first occurrence of the value 2 using arr.index(2).
The new created array is : 1 2 3 1 2 5 The index of 1st occurrence of 2 is : 1
reverse() method is used to reverse the elements of an array in place.
Example: In this example, the reverse() method is applied to invert the order of elements in-place and finally, reversed array is printed.
The new created array is : 1 2 3 1 2 5 The array after reversing is : 5 2 1 3 2 1