Arrays in Python
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]) # 30Characteristics 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 arrayCreating 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.
Why Use Typed Arrays Instead of List?
Accessing Elements
- Positive Indexing
arr = [10,20,30,40]
print(arr[1])
# 202. Negative Indexing
arr = [10,20,30,40]
print(arr[-1]) # 40Traversing 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
# 40Updating Elements
arr = [10, 20, 30]
arr[1] = 25
print(arr)
# ouput:
# [10, 25, 30]Adding Elements
- append() — Add at the End
- Adds a single element to the end of the array.
arr.append(40)Get Saurabh Gandhi’s stories in your inbox
Join Medium for free to get updates from this writer.
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
- 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,
arraydoes NOT have direct.clear()method.
arr = array('i') # ReinitializeInternal Working (Interview Important)
- Arrays store elements in contiguous memory
- When removing an element: All elements after it are shifted left
Questions
- What is an array in Python?
- How is list different from array module?
- Why are Python lists called dynamic arrays?
- What is the difference between append() and extend()?
- Can we insert different data types in Python array?
- What is the difference between
remove()andpop()? - Can we remove elements using slicing?










