Array is a most fundamental and useful data structure in Python Programming Language. Python programming provides the array
module, which allows to create arrays. Array
is a sequential collection of elements of homogeneous datatype that are stored at contiguous memory locations. Array
in Python are more memory-efficient than lists. Python Arrays are very helpful to store large amounts of homogeneous data (data of the same datatype).
Properties of Array in Python
Python arrays has specific properties which makes them more efficient that lists. These properties include type constraint, memory efficiency, fixed size, and more.
- Homogeneous Data – Python arrays support only single datatype elements. All elements stored in an array, should be of specific datatype (e.g., integers, floating-point numbers, characters).
- Type Codes – Array elements datatype is specified by the Type Codes. For example,
'i'
for integers and'f'
for floating-point numbers. - Memory Optimized Storage – Arrays in Python are more memory-efficient than lists. This is very helpful in handling of large amounts of data. Because of uniform datatype elements in array, Python can allocate memory more efficiently, reducing overhead.
- Contiguous Memory Layout – Array elements are stored sequentially in memory. This helps to improve data cache and performance of applications.
- Fixed Size – Specific size (number of elements) is defined when an array object is created and memory allocation is also based on the defined number of elements.
- Dynamic Resize – Append or insert operations are allowed on array object, but this operation will involve reallocation of memory. This dynamic resize reduces performance of arrays and make them less efficient than lists.
- Indexing – Arrays are indexed and its elements can be accessed based on their index positions. Arrays can achieve O(1) time complexity for accessing elements by index.
- Basic Operations: Python arrays support a range of operations similar to lists, such as slicing, concatenation, and iteration except some of the advanced functionalities provided by lists.
- Immutable Type Code – Arrays type code is immutable and cannot be changed. It means, once an array is created with a specific type code, we cannot change the type code for this array. We can delete this array and can create a new array with different type code.
- 1D Arrays – By default, Python built-in array module supports only one-dimensional arrays. For multidimensional arrays support we can use Python library NumPy.
- Interoperability with C – Python arrays can easily interacting with C libraries can be very helpful to pass data to and from C code.
Index of an element in Array
Elements in the array
are stored sequentially based on their index positions. Index assignment to array
elements is similar to index assigned in List elements.
Two ways in which index can be assigned to array elements:
- Index from left to right (Positive Indexing)
- Index from right to left (Negative Indexing)
Array Type Codes
Array elements datatype is specified by the Type Codes. An array in Python can only store elements of specified type code. Array type codes are single-character and define the type of elements. Below is a list of common array type codes: