Implementation of Dynamic Array in Python
Last Updated :
12 May, 2023
What is a dynamic array?
A dynamic array is similar to an array, but with the difference that its size can be dynamically modified at runtime. Don't need to specify how much large an array beforehand. The elements of an array occupy a contiguous block of memory, and once created, its size cannot be changed. A dynamic array can, once the array is filled, allocate a bigger chunk of memory, copy the contents from the original array to this new space, and continue to fill the available slots.

We'll be using a built in library called ctypes of python . Check out the documentation for more info, but its basically going to be used here as a raw array from the ctypes module.
A quick note on public vs private methods, we can use an underscore _ before the method name to keep it non-public.
For example:
Python
class M(object):
def public(self):
print 'Use Tab to see me !'
def _private(self):
print "You won't be able to Tab to see me !"
Python
Output:
Use Tab to see me!
Python
Output:
You won't be able to see me!
Dynamic Array Logic Implementation:
The key is to provide means to grows an array A that stores the elements of a list. We can't actually grow the array, its capacity is fixed. If an element is appended to a list at a time, when the underlying array is full, we need to perform following steps.
- Allocate a new array B with larger capacity (A commonly used rule for the new array is to have twice the capacity of the existing array )
- Set B[i]=A[i], for i=0 to n-1 where n denotes the current no of items.
- Set A=B that is, we hence forth use B as the array of supporting list.
- Insert new element in the new array.

Dynamic Array Code Implementation:
Python3
import ctypes
class DynamicArray(object):
'''
DYNAMIC ARRAY CLASS (Similar to Python List)
'''
def __init__(self):
self.n = 0 # Count actual elements (Default is 0)
self.capacity = 1 # Default Capacity
self.A = self.make_array(self.capacity)
def __len__(self):
"""
Return number of elements stored in array
"""
return self.n
def __getitem__(self, k):
"""
Return element at index k
"""
if not 0 <= k < self.n:
# Check it k index is in bounds of array
return IndexError('K is out of bounds !')
return self.A[k] # Retrieve from the array at index k
def append(self, ele):
"""
Add element to end of the array
"""
if self.n == self.capacity:
# Double capacity if not enough room
self._resize(2 * self.capacity)
self.A[self.n] = ele # Set self.n index to element
self.n += 1
def insertAt(self, item, index):
"""
This function inserts the item at any specified index.
"""
if index < 0 or index > self.n:
print("please enter appropriate index..")
return
if self.n == self.capacity:
self._resize(2*self.capacity)
for i in range(self.n-1, index-1, -1):
self.A[i+1] = self.A[i]
self.A[index] = item
self.n += 1
def delete(self):
"""
This function deletes item from the end of array
"""
if self.n == 0:
print("Array is empty deletion not Possible")
return
self.A[self.n-1] = 0
self.n -= 1
def removeAt(self, index):
"""
This function deletes item from a specified index..
"""
if self.n == 0:
print("Array is empty deletion not Possible")
return
if index < 0 or index >= self.n:
return IndexError("Index out of bound....deletion not possible")
if index == self.n-1:
self.A[index] = 0
self.n -= 1
return
for i in range(index, self.n-1):
self.A[i] = self.A[i+1]
self.A[self.n-1] = 0
self.n -= 1
def _resize(self, new_cap):
"""
Resize internal array to capacity new_cap
"""
B = self.make_array(new_cap) # New bigger array
for k in range(self.n): # Reference all existing values
B[k] = self.A[k]
self.A = B # Call A the new bigger array
self.capacity = new_cap # Reset the capacity
def make_array(self, new_cap):
"""
Returns a new array with new_cap capacity
"""
return (new_cap * ctypes.py_object)()
# Instantiate
arr = DynamicArray()
# append the new elements
arr.append(1)
arr.append(2)
arr.append(3)
# length of the given append in array
print(len(arr))
# access the given append in array
print(arr[1])
print(arr[2])
# remove the given the array
arr.removeAt(2)
# length of the array
print(len(arr))
# index of the array
print(arr[1])
#This code Contribute by Raja.Ramakrishna
Output:
3
2
3
2
2
Similar Reads
Internal implementation of Data Structures in Python Python provides a variety of built-in data structures, each with its own characteristics and internal implementations optimized for specific use cases. In this article we are going to discuss about the most commonly used Data structures in Python and a brief overview of their internal implementation
3 min read
Declaring an Array in Python An array is a container used to store the same type of elements such as integer, float, and character type. An Array is one of the most important parts of data structures. In arrays, elements are stored in a contiguous location in a memory. We can access the array elements by indexing from 0 to (siz
4 min read
pandas.array() function in Python This method is used to create an array from a sequence in desired data type. Syntax : pandas.array(data: Sequence[object], dtype: Union[str, numpy.dtype, pandas.core.dtypes.base.ExtensionDtype, NoneType] = None, copy: bool = True) Parameters : data : Sequence of objects. The scalars inside `data` sh
2 min read
Python: Operations on Numpy Arrays NumPy is a Python package which means 'Numerical Python'. It is the library for logical computing, which contains a powerful n-dimensional array object, gives tools to integrate C, C++ and so on. It is likewise helpful in linear based math, arbitrary number capacity and so on. NumPy exhibits can lik
3 min read
Array in Python | Set 2 (Important Functions) Array in Python | Set 1 (Introduction and Functions)Array in Python | Set 2Below are some more useful functions provided in Python for arrays: Array Typecode FunctionThis function returns the data type by which the array is initialized. In this example, we are using arr.typecode to find out the data
3 min read
bytearray() function - Python The bytearray() function in Python creates a mutable sequence of bytes, which is essentially an array of integers in the range 0 to 255 (representing byte values). Unlike the immutable bytes type, bytearray allows us to modify its contents after creation, making it useful for tasks like binary data
5 min read
Python | Extension function operating on Arrays Let's write a C extension function that can operate on contiguous arrays of data, as might be created by the array module or libraries like NumPy and this function should be general purpose and not specific to any one array library. The code should use Buffer Protocol to receive and process arrays i
2 min read
Create Classes Dynamically in Python A class defines a collection of instance variables and methods to specify an object type. A class can be used to make as many object instances of the type of object as needed. An object is an identified entity with certain attributes (data members) and behaviours (member functions). Group of objects
2 min read
Python Array length Finding the length of an array in Python means determining how many elements are present in the array. For example, given an array like [1, 2, 3, 4, 5], you might want to calculate the length, which is 5. Let's explore different methods to efficiently. Using len()len() function is the most efficient
2 min read
Python - Built-in array vs NumPy array Let us concentrate on the built-in array module first. Built-in array module defines an object type which can efficiently represent an array of basic values: characters, integers, floating point numbers. Arrays are sequence types and behave very much like lists, except that the type of objects store
5 min read