Python Arrays: Creation, Manipulation, and Advanced Operations – Expert Guide
What is a Python Array?
A Python array is a homogeneous collection of elements stored in contiguous memory, offering efficient storage and faster numerical operations compared to lists. The built‑in array module handles array creation and manipulation.
When to Use Python Arrays
Python arrays are ideal when you need to store many values of the same type and perform numerical computations. They consume less memory and provide faster access than lists, especially for large datasets.
Syntax to Create an Array
Import the array module and call array.array(typecode, initializer):
import array
my_array = array.array('i', [1, 2, 3])
Array Type Codes
| Type code | Python type | C Type | Min size (bytes) |
|---|---|---|---|
| 'u' | Unicode character | Py_UNICODE | 2 |
| 'b' | Int | Signed char | 1 |
| 'B' | Int | Unsigned char | 1 |
| 'h' | Int | Signed short | 2 |
| 'l' | Int | Signed long | 4 |
| 'L' | Int | Unsigned long | 4 |
| 'q' | Int | Signed long long | 8 |
| 'Q' | Int | Unsigned long long | 8 |
| 'H' | Int | Unsigned short | 2 |
| 'f' | Float | Float | 4 |
| 'd' | Float | Double | 8 |
| 'i' | Int | Signed int | 2 |
| 'I' | Int | Unsigned int | 2 |
Accessing Elements
Elements are accessed by zero‑based index:
import array
balance = array.array('i', [300, 200, 100])
print(balance[1]) # 200
You can also use negative indices or slicing:
import array
arr = array.array('q', [3, 9, 6, 5, 20, 13, 19, 22, 30, 25])
print(arr[1:4]) # array('q', [9, 6, 5])
print(arr[7:10]) # array('q', [22, 30, 25])
Inserting Elements
Insert a value at a specific position with insert(index, value):
import array
balance = array.array('i', [300, 200, 100])
balance.insert(2, 150)
print(balance) # array('i', [300, 200, 150, 100])
Modifying Elements
Arrays are mutable; assign a new value directly:
import array
arr = array.array('b', [3, 6, 4, 8, 10])
arr[0] = 99
print(arr) # array('b', [99, 6, 4, 8, 10])
Concatenation is also supported:
import array
first = array.array('b', [4, 6, 8])
second = array.array('b', [9, 12, 15])
combined = first + second
print(combined) # array('b', [4, 6, 8, 9, 12, 15])
Removing Elements
Pop removes an item by index:
import array
arr = array.array('b', [20, 25, 30])
arr.pop(2)
print(arr) # array('b', [20, 25])
Del deletes an element by position:
import array
arr = array.array('b', [10, 4, 5, 5, 7])
del arr[4]
print(arr) # array('b', [10, 4, 5, 5])
Remove deletes the first occurrence of a value:
import array
arr = array.array('b', [2, 3, 4])
arr.remove(3)
print(arr) # array('b', [2, 4])
Searching and Indexing
Find the index of a value with index(value):
import array
arr = array.array('b', [2, 3, 4, 5, 6])
print(arr.index(3)) # 1
Reversing an Array
Reverse in place:
import array
arr = array.array('b', [1, 2, 3])
arr.reverse()
print(arr) # array('b', [3, 2, 1])
Converting to Unicode
Only arrays of type 'u' support tounicode():
from array import array
p = array('u', [u'\u0050', u'\u0059', u'\u0054', u'\u0048', u'\u004F', u'\u004E'])
print(p) # array('u', 'PYTHON')
print(p.tounicode()) # PYTHON
Counting Elements
Count occurrences with count(x):
import array
arr = array.array('b', [2, 3, 5, 4, 3, 3, 3])
print(arr.count(3)) # 4
Traversing an Array
Iterate with a for‑loop:
import array
arr = array.array('i', [300, 200, 100])
for x in arr:
print(x)
Summary
- A Python array holds elements of a single type, defined by a type code.
- Arrays are created via the
arraymodule and are more memory‑efficient than lists. - Use indexing, slicing, and built‑in methods (
insert,remove,pop,reverse) to manipulate data. - Arrays support concatenation, counting, and searching.
- Only
'u'arrays can be converted to Unicode strings.
Python
- Python Print() Function: A Practical Guide with Examples
- Master Python’s str.count(): How to Count Characters & Substrings with Examples
- Master Python `format()` Strings with Clear Examples
- Master Python's String.find() Method: Syntax, Examples & Alternatives
- Master Python Lambda Functions: Practical Examples & Best Practices
- Python round() Function Explained with Practical Examples
- Mastering Python's map() Function: Syntax, Examples, and Best Practices
- Python timeit() – Measuring Execution Time with Practical Examples
- Python list.count(): Expert Guide with Practical Examples
- Python Module Importing – A Practical Guide with Examples