Sitemap

Arrays in Python

3 min readMar 9, 2026

--

In Python, arrays are used to store multiple values in a single variable. However, Python does not have a built-in array type like C or Java. Instead, Python mainly uses:

  • List (most common)
  • array module
  • NumPy arrays (for numerical computing)

What is an Array?

An array is a data structure that:

  • Stores multiple elements in a single variable
  • Elements are stored in sequential memory locations
  • Accessed using index numbers
  • Usually stores elements of the same type

Arrays Using Python Lists (Dynamic Arrays)

In Python, lists act as dynamic arrays.

arr = [10, 20, 30, 40]

print(arr[0]) # 10
print(arr[2]) # 30

Characteristics of List-Based Arrays

  • Ordered
  • Mutable (can change values)
  • Dynamic size (can grow/shrink)
  • Allows duplicate values
  • Can store multiple data types

The array Module in Python (Typed Arrays)

Python provides the array module to create typed arrays (arrays that store elements of the same data type).

from array import array

Creating a Typed Array

array(typecode, iterable)
from array import array
arr = array("i", [10,20,30,40])
print(arr)
# ouput
# array('i', [10, 20, 30, 40])

Type Codes

  • Type code defines what data type the array can store.
Press enter or click to view image in full size

Why Use Typed Arrays Instead of List?

Press enter or click to view image in full size

Accessing Elements

  1. Positive Indexing
arr = [10,20,30,40]
print(arr[1])
# 20

2. Negative Indexing

arr = [10,20,30,40]
print(arr[-1]) # 40

Traversing an Array

  • Using for loop
arr = [10,20,30,40]
for element in arr:
print(element)
# ouput
# 10
# 20
# 30
# 40
  • Using index
arr = [10,20,30,40]
for i in range(len(arr)):
print(arr[i])
# ouput
# 10
# 20
# 30
# 40

Updating Elements

arr = [10, 20, 30]
arr[1] = 25
print(arr)
# ouput:
# [10, 25, 30]

Adding Elements

  1. append() — Add at the End
  • Adds a single element to the end of the array.

Get Saurabh Gandhi’s stories in your inbox

Join Medium for free to get updates from this writer.

arr.append(40)
print(arr)
  • Used when you want to grow array from the end.

2. insert() — Add at Specific Position

arr.insert(1, 15)   # Insert 15 at index 1
print(arr)
  • Useful when position matters.

3. extend() — Add Multiple Elements

  • Adds multiple elements at once.
arr.extend([50, 60])
print(arr)
  • Efficient for bulk insertion.

Important Rules

  • You can only add elements of the same data type.
  • If array type is ‘i’ (integer), you cannot add float or string.

Internal Working

Python arrays:

  • Are stored in contiguous memory
  • Have fixed data type
  • When size increases → memory may be reallocated internally

Removing Elements

  1. remove() — Remove by Value
  • Removes the first occurrence of a value.
arr.remove(30)
print(arr)

2. pop() — Remove by Index

  • Removes element using index position.
  • If no index is given → removes last element
arr.pop(1)
print(arr)

3. del Statement — Delete by Index or Slice

  • Delete single element
del arr[0]
  • Delete multiple elements (slicing)
del arr[1:3]

4. clear() — Remove All Elements

  • Unlike list, array does NOT have direct .clear() method.
arr = array('i')   # Reinitialize

Internal Working (Interview Important)

  • Arrays store elements in contiguous memory
  • When removing an element: All elements after it are shifted left

Questions

  1. What is an array in Python?
  2. How is list different from array module?
  3. Why are Python lists called dynamic arrays?
  4. What is the difference between append() and extend()?
  5. Can we insert different data types in Python array?
  6. What is the difference between remove() and pop()?
  7. Can we remove elements using slicing?

--

--

Saurabh Gandhi
Saurabh Gandhi

Python • AI • Tech Learning Writing simple guides that make coding easy for everyone.