Python Arrays
The array module provides type-specific arrays that are more memory-efficient than lists when storing homogeneous numeric data. You must specify the type code (e.g., 'i' for signed int, 'f' for float).
Creating an Array
Import the array module and pass a type code and an initial list.
from array import array
nums = array('i', [10, 20, 30, 40, 50])
print(nums)
print(nums[2])
Try it Yourself →
Array Methods
Arrays support similar methods to lists: append(), extend(), insert(), remove(), pop(), reverse().
from array import array
nums = array('f', [1.5, 2.5, 3.5])
nums.append(4.5)
nums.insert(0, 0.5)
nums.pop()
print(nums)
Try it Yourself →