Python Programming Arrays
Type codes Memory efficient

Python array Module

Space-efficient array.array sequences: type codes, operations, memory vs lists, and when to prefer lists or NumPy.

Homogeneous

One type per array

vs Lists

Mixed vs typed

NumPy

Math & n-D

Operations

append, slice, …

What is the Array Module?

The array module in Python provides a space-efficient way to store homogeneous data types (all elements must be the same type). Unlike lists, arrays are more memory efficient but less flexible.

Importing the Array Module

Import styles
import array

# Or import specific types
from array import array

Type Codes for Arrays

Array elements must be the same type, specified by a type code:

Type Code C Type Python Type Minimum Size (bytes)
'b'signed charint1
'B'unsigned charint1
'u'wchar_tUnicode char2
'h'signed shortint2
'H'unsigned shortint2
'i'signed intint2
'I'unsigned intint2
'l'signed longint4
'L'unsigned longint4
'q'signed long longint8
'Q'unsigned long longint8
'f'floatfloat4
'd'doublefloat8

Exact sizes can vary by platform; treat the table as a guide. Type 'u' is deprecated in favor of other representations in modern Python—prefer str or appropriate codecs for text.

Creating Arrays

Create arrays with different type codes
import array

# Create arrays with different type codes
int_array = array.array('i', [1, 2, 3, 4, 5])
float_array = array.array('f', [1.5, 2.5, 3.5])
char_array = array.array('u', ['a', 'b', 'c'])

# Empty array
empty_array = array.array('i')

print(int_array)    # array('i', [1, 2, 3, 4, 5])
print(float_array)  # array('f', [1.5, 2.5, 3.5])

Key Differences from Lists

1. Homogeneous vs Heterogeneous

Mixed types vs uniform type
# List can mix types
list_data = [1, "hello", 3.14, True]  # Works fine

# Array must have uniform type
from array import array
array_data = array('i', [1, 2, 3])     # Works
# array_data = array('i', [1, "hello", 3])  # TypeError!

2. Memory Efficiency

Approximate footprint (values vary by build)
import sys
from array import array

# List takes more memory
list_numbers = [1, 2, 3, 4, 5]
print(sys.getsizeof(list_numbers))  # ~120 bytes (depending on system)

# Array takes less memory
array_numbers = array('i', [1, 2, 3, 4, 5])
print(sys.getsizeof(array_numbers))  # ~100 bytes (depending on system)

Array Operations

Basic Operations

Access and slice
from array import array

# Creating
arr = array('i', [10, 20, 30, 40, 50])

# Accessing elements
print(arr[0])     # 10
print(arr[-1])    # 50
print(arr[2:4])   # array('i', [30, 40])

# Modifying elements
arr[1] = 25
print(arr)  # array('i', [10, 25, 30, 40, 50])

Adding Elements

append, extend, insert
from array import array

arr = array('i', [1, 2, 3])

# Append to end
arr.append(4)
print(arr)  # array('i', [1, 2, 3, 4])

# Extend with another iterable
arr.extend([5, 6, 7])
print(arr)  # array('i', [1, 2, 3, 4, 5, 6, 7])

# Insert at position
arr.insert(2, 99)
print(arr)  # array('i', [1, 2, 99, 3, 4, 5, 6, 7])

Removing Elements

remove, pop
from array import array

arr = array('i', [10, 20, 30, 20, 40])

# Remove by value (first occurrence only)
arr.remove(20)
print(arr)  # array('i', [10, 30, 20, 40])

# Remove by index
popped = arr.pop(2)
print(popped)  # 20
print(arr)     # array('i', [10, 30, 40])

# Remove last element
arr.pop()
print(arr)  # array('i', [10, 30])

Searching

index, count, membership
from array import array

arr = array('i', [10, 20, 30, 40, 20, 50])

# Find index of first occurrence
index = arr.index(20)
print(index)  # 1

# Count occurrences
count = arr.count(20)
print(count)  # 2

# Check if value exists
if 30 in arr:
    print("Found 30")  # This prints

Array Information

len, typecode, buffer_info, itemsize
from array import array

arr = array('i', [10, 20, 30, 40, 50])

# Length
print(len(arr))  # 5

# Get type code
print(arr.typecode)  # 'i'

# Get buffer size in bytes
print(arr.buffer_info())  # (address, length)
print(arr.itemsize)  # 4 (bytes per item for type 'i')

Converting Between Arrays and Lists

tolist, fromlist, bytes
from array import array

# List to array
my_list = [1, 2, 3, 4, 5]
my_array = array('i', my_list)

# Array to list
my_list2 = my_array.tolist()
print(my_list2)  # [1, 2, 3, 4, 5]

# Array to string/bytes (for 'b' type)
byte_array = array('b', [65, 66, 67, 68])
string_from_bytes = byte_array.tobytes()
print(string_from_bytes)  # b'ABCD'

Methods Summary

Method Description
append(x)Adds x to the end
count(x)Returns occurrences of x
extend(iterable)Appends all items from iterable
frombytes(bytes)Appends items from bytes object
fromlist(list)Appends items from list
fromunicode(s)Appends chars from unicode string ('u' type; deprecated)
index(x)Returns index of first x
insert(i, x)Inserts x at position i
pop([i])Removes and returns item at i (default last)
remove(x)Removes first occurrence of x
reverse()Reverses order of items
tolist()Converts array to list
tobytes()Converts array to bytes
tounicode()Converts array to unicode string ('u' type)

Practical Example: Storing Sensor Data

Temperature readings
from array import array
import random

# Store temperature readings (floats)
temperatures = array('f')

# Simulate 100 sensor readings
for _ in range(100):
    temp = random.uniform(18.5, 25.5)
    temperatures.append(temp)

print(f"Recorded {len(temperatures)} readings")
print(f"First 5 readings: {temperatures[:5]}")
print(f"Memory used: {temperatures.buffer_info()[1] * temperatures.itemsize} bytes")

# Convert to list for more complex operations
temp_list = temperatures.tolist()
average = sum(temp_list) / len(temp_list)
print(f"Average temperature: {average:.2f}°C")

When to Use Array Module vs Lists vs NumPy

Use array when
  • You need memory efficiency for large homogeneous data
  • You're working with numeric data types
  • You need to interface with C code
  • You want to control the exact data type storage size
Use Lists when
  • You need flexibility with mixed data types
  • You're working with small datasets
  • You need advanced list methods like sorting
  • Readability is more important than memory efficiency
Use NumPy when
  • You need mathematical operations on arrays
  • You're doing scientific computing
  • You need multidimensional arrays
  • Performance is critical for large datasets

How it fits together

The array module fills the gap between lists and NumPy — more efficient than lists for homogeneous data, but less powerful than NumPy for mathematical operations.

Key Takeaways

  • The array module stores homogeneous data compactly; lists allow mixed types and more convenience APIs (Lists tutorial).
  • Choose a type code that matches your data; wrong types raise errors.
  • Use append, extend, insert, remove, pop, index, and count like other mutable sequences.
  • buffer_info() and itemsize describe raw storage; tolist() / fromlist() bridge to lists.
  • For large numeric or matrix work, install and use NumPy; use array when you want stdlib-only, typed buffers.
Next Topics: Continue to Python Strings — text sequences, formatting, and string methods (after loops → arrays → strings in this course path).