Skip to main content

Overview

The Array class provides a static array implementation with a fixed capacity. Unlike Python’s built-in list, this array has a predetermined maximum size and will raise exceptions if capacity is exceeded.

Special Methods

  • Index Operator: array[index] - Access elements by index
  • Assignment: array[index] = value - Set values at specific indices
  • Equality: Array instances can be compared for equality with other Array or DynamicArray instances based on their contents

Constructor

__init__(contents=None, capacity: int=10)

Initialize the array with optional contents and a fixed capacity.
contents
iterable
default:"None"
An optional iterable to fill the array with initial values. If the length of contents exceeds the specified capacity, the capacity will be automatically adjusted to fit.
capacity
int
default:"10"
The initial size of the array. Determines the maximum number of elements the array can hold.
from dsa.array import Array

# Create an empty array with default capacity of 10
arr = Array()

# Create an array with specific capacity
arr = Array(capacity=20)

# Create an array with initial contents
arr = Array(contents=[1, 2, 3, 4, 5])

# Create with contents larger than default capacity
arr = Array(contents=range(15))  # capacity automatically set to 15

Methods

append(element)

Append an element to the end of the array.
element
any
required
The element to append to the array.
Raises:
  • Exception: If the array is full and capacity is exceeded
Time Complexity: O(1) Space Complexity: O(1)
arr = Array(capacity=5)
arr.append(10)
arr.append(20)
arr.append(30)
print(arr)  # [10, 20, 30] Count: 3 Capacity: 5

extend(array)

Append multiple elements from a given iterable.
array
iterable
required
An iterable containing elements to append to the array.
Raises:
  • Exception: If appending the elements exceeds the array’s capacity
Time Complexity: O(n) where n is the number of elements to add Space Complexity: O(1)
arr = Array(capacity=10)
arr.extend([1, 2, 3, 4, 5])
print(arr)  # [1, 2, 3, 4, 5] Count: 5 Capacity: 10

# Extend with another iterable
arr.extend(range(6, 9))
print(arr)  # [1, 2, 3, 4, 5, 6, 7, 8] Count: 8 Capacity: 10

insert(index: int, element)

Insert an element at a specified index, shifting existing elements to the right.
index
int
required
The index at which to insert the element. Must be between 0 and the current count (inclusive).
element
any
required
The element to insert.
Raises:
  • IndexError: If the index is out of bounds
  • Exception: If the array is full and cannot accommodate the shift
Time Complexity: O(n) where n is the number of elements to shift Space Complexity: O(1)
arr = Array(contents=[1, 2, 4, 5], capacity=10)
arr.insert(2, 3)  # Insert 3 at index 2
print(arr)  # [1, 2, 3, 4, 5] Count: 5 Capacity: 10

# Insert at the beginning
arr.insert(0, 0)
print(arr)  # [0, 1, 2, 3, 4, 5] Count: 6 Capacity: 10

delete(index: int)

Delete an element at a specified index, shifting subsequent elements to the left.
index
int
required
The index of the element to delete. Must be between 0 and count - 1.
Raises:
  • IndexError: If the index is out of bounds
Time Complexity: O(n) where n is the number of elements to shift Space Complexity: O(1)
arr = Array(contents=[1, 2, 3, 4, 5])
arr.delete(2)  # Delete element at index 2
print(arr)  # [1, 2, 4, 5] Count: 4 Capacity: 5

# Delete the first element
arr.delete(0)
print(arr)  # [2, 4, 5] Count: 3 Capacity: 5

shift_right(start: int)

Helper method to shift elements to the right from a specified start index until the last element. This is primarily used internally by the insert method.
start
int
required
The index at which to start shifting (inclusive).
Raises:
  • Exception: If the array is full and cannot accommodate the shift
This method may delete an element but does not affect the count. It is typically used internally and not called directly by users.
Time Complexity: O(n) where n is the number of elements to shift Space Complexity: O(1)

shift_left(start: int)

Helper method to shift elements to the left starting at a start index. This is primarily used internally by the delete method.
start
int
required
The starting index of the shift.
This method may delete an element but does not affect the count. It is typically used internally and not called directly by users.
Time Complexity: O(n) where n is the number of elements to shift Space Complexity: O(1)

is_empty() -> bool

Check if the array is empty.
return
bool
Returns True if the array contains no elements, False otherwise.
Time Complexity: O(1) Space Complexity: O(1)
arr = Array()
print(arr.is_empty())  # True

arr.append(1)
print(arr.is_empty())  # False

capacity() -> int

Get the total capacity of the array.
return
int
The maximum number of elements the array can hold.
Time Complexity: O(1) Space Complexity: O(1)
arr = Array(capacity=20)
print(arr.capacity())  # 20

arr.append(1)
print(arr.capacity())  # 20 (capacity doesn't change)
print(len(arr))        # 1 (but count does)

to_list() -> list

Convert the array’s elements to a standard Python list.
return
list
A list containing only the active elements of the array (not including unused capacity).
Time Complexity: O(n) where n is the number of elements Space Complexity: O(n)
arr = Array(contents=[1, 2, 3, 4, 5], capacity=10)
py_list = arr.to_list()
print(py_list)  # [1, 2, 3, 4, 5]
print(type(py_list))  # <class 'list'>

from_list(mylist: list) (classmethod)

Create an array from a standard Python list.
mylist
list
required
A Python list to initialize the array with.
return
Array
An instance of the Array class with capacity equal to the length of the input list.
Time Complexity: O(n) where n is the length of the list Space Complexity: O(n)
py_list = [10, 20, 30, 40, 50]
arr = Array.from_list(py_list)
print(arr)  # [10, 20, 30, 40, 50] Count: 5 Capacity: 5

Special Methods

__getitem__(index: int)

Retrieve the element at the specified index. Enables the use of bracket notation array[index].
index
int
required
The index of the element to retrieve. Must be between 0 and count - 1.
return
any
The element at the specified index.
Raises:
  • IndexError: If the index is out of bounds
Time Complexity: O(1) Space Complexity: O(1)
arr = Array(contents=[10, 20, 30, 40, 50])
print(arr[0])   # 10
print(arr[2])   # 30
print(arr[4])   # 50

try:
    print(arr[10])  # IndexError
except IndexError:
    print("Index out of bounds")

__setitem__(index: int, value)

Set a new value at the specified index. Enables the use of bracket notation array[index] = value.
index
int
required
The index at which to set the value. Must be between 0 and count - 1.
value
any
required
The new value to assign.
Raises:
  • IndexError: If the index is out of bounds
Time Complexity: O(1) Space Complexity: O(1)
arr = Array(contents=[10, 20, 30, 40, 50])
arr[2] = 100
print(arr)  # [10, 20, 100, 40, 50] Count: 5 Capacity: 5

arr[0] = 5
print(arr[0])  # 5

__len__() -> int

Return the number of elements in the array. Enables the use of len(array).
return
int
The number of active elements in the array (not the capacity).
Time Complexity: O(1) Space Complexity: O(1)
arr = Array(capacity=20)
print(len(arr))  # 0

arr.append(1)
arr.append(2)
print(len(arr))  # 2

__repr__() -> str

Represent the array’s contents, count, and capacity as a string.
return
str
A string representation showing the array’s elements, current count, and total capacity.
Time Complexity: O(n) where n is the number of elements Space Complexity: O(n)
arr = Array(contents=[1, 2, 3], capacity=10)
print(arr)  # [1, 2, 3] Count: 3 Capacity: 10

__eq__(other) -> bool

Compare this array to another for equality.
other
any
required
The object to compare with.
return
bool | NotImplemented
Returns True if both objects are Array, DynamicArray, or CircularArray instances and their contents are equal. For non-array types, returns NotImplemented to allow reverse comparison.
Time Complexity: O(n) where n is the number of elements Space Complexity: O(1)
arr1 = Array(contents=[1, 2, 3])
arr2 = Array(contents=[1, 2, 3])
arr3 = Array(contents=[1, 2, 4])

print(arr1 == arr2)  # True
print(arr1 == arr3)  # False

# Works with DynamicArray too
from dsa.array import DynamicArray
dyn_arr = DynamicArray(contents=[1, 2, 3])
print(arr1 == dyn_arr)  # True

Attributes

count

count
int
The number of elements currently stored in the array. This is updated automatically when elements are added or removed.
arr = Array(capacity=10)
print(arr.count)  # 0

arr.append(1)
print(arr.count)  # 1

arr.extend([2, 3, 4])
print(arr.count)  # 4

Build docs developers (and LLMs) love