Python Arrays
An Array is a collection of similar type of data.
Array in Python can be created by importing the array module.
Syntax
array(data type, [array list])
Example
from array import array
nums = array("i", [1, 3, 44, 56, 77, 8])
print("int nums = ", end = "")
for i in range(len(nums)):
print(nums[i], end = " ")
print()
nums = array("d", [1.3, 3.5, 44.46, 56.3])
print("double nums = ", end = "")
for i in range(len(nums)):
print(nums[i], end = " ")
Output
double nums = 1.3 3.5 44.46 56.3
While creating an array, the first argument you need to provide is the data type of the array values. The following table will help you create arrays of different types:
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' | unsigned long | int | 4 |
'L' | unsigned long | int | 4 |
'f' | float | int | 4 |
'd' | double | int | 8 |
Array Methods
Some of the useful array methods are given below:
Method | Description |
append() | Adds an item at the end of an array. |
clear() | Removes all items from an array. |
count() | Returns the number of items in an array. |
copy() | Return copy of an array. |
extend() | Adds multiple items at the end of an array. |
insert() | Adds an item at the specified index of an array. |
pop() | Removes an item from the specified index of an array. |
remove() | Removes the specified item from an array. |
reverse() | Reverses the order of items in an array. |
sort() | Sorts the order of items in an array. |
Access Items of an Array
To access items of array, you can use the square brackets with index.
Syntax
array[index]
Example
from array import array
nums = array("i", [1, 3, 44, 56, 77, 8])
print(nums[2])
Output
Add Item in an Array
To add item in an array, you can use the append() method.
Example
from array import array
nums = array("i", [1, 3, 44, 56, 77, 8])
print("nums before = ", end = "")
for i in range(len(nums)):
print(nums[i], end = " ")
print()
#add new value
nums.append(55)
print("nums after = ", end = "")
for i in range(len(nums)):
print(nums[i], end = " ")
Output
nums after = 1 3 44 56 77 8 55
Insert Item in an Array
To insert an item at the specified index of an array, you can use the insert() method.
Syntax
insert(index, item)
Example
from array import array
nums = array("i", [1, 3, 44, 56, 77, 8])
print("nums before = ", end = "")
for i in range(len(nums)):
print(nums[i], end = " ")
print()
#add new value
nums.insert(0,55)
print("nums after = ", end = "")
for i in range(len(nums)):
print(nums[i], end = " ")
Output
nums after = 55 3 44 56 77 8
Remove Item from an Array
To remove an item from an array, you can use the remove() method.
Example
from array import array
nums = array("i", [1, 3, 44, 56, 77, 8])
print("nums before = ", end = "")
for i in range(len(nums)):
print(nums[i], end = " ")
print()
#remove value
nums.remove(77)
print("nums after = ", end = "")
for i in range(len(nums)):
print(nums[i], end = " ")
Output
nums after = 1 3 44 56 8
Pop Item from an Array
To remove an item at the specified index of an array, you can use the pop() method.
Syntax
pop(index)
Example
from array import array
nums = array("i", [1, 3, 44, 56, 77, 8])
print("nums before = ", end = "")
for i in range(len(nums)):
print(nums[i], end = " ")
print()
#remove value at specified index
nums.pop(2)
print("nums after = ", end = "")
for i in range(len(nums)):
print(nums[i], end = " ")
Output
nums after = 1 3 56 77 8