Python Arrays – Efficient Data Storage with array Module
Introduction – What Are Arrays in Python?
In Python, an array is a container that holds multiple values of the same data type in a single variable. While Python lists can store mixed types, arrays offer better performance, memory efficiency, and are especially suited for numerical computations.
Python provides arrays through:
- The built-in
arraymodule for basic, fixed-type arrays - NumPy arrays (not covered here) for advanced numerical computing
In this guide, you’ll learn:
- How to create and use arrays using the
arraymodule - Type codes, slicing, appending, and removing
- Arrays vs lists
- Best practices and real-world use cases
Importing the Array Module
from array import array
Explanation:
- The
arraymodule must be imported before creating array objects.
Type Codes for Array Creation
| Type Code | C Type | Python Type | Description |
|---|---|---|---|
'i' | signed int | int | Standard integers |
'f' | float | float | 32-bit floating point |
'd' | double float | float | 64-bit floating point |
'u' | Unicode char | str (1 char) | Single Unicode char |
Creating an Array
nums = array('i', [1, 2, 3, 4])
print(nums)
Explanation:
'i'defines an array of signed integers.- Output:
array('i', [1, 2, 3, 4])
Accessing Array Elements
print(nums[0]) # Output: 1
print(nums[-1]) # Output: 4
Explanation:
- Indexing works like lists: positive for start, negative for reverse.
Updating Array Elements
nums[1] = 20
print(nums) # Output: array('i', [1, 20, 3, 4])
Explanation:
- Updates the value at index 1 to
20.
Adding Elements
Using .append()
nums.append(5)
print(nums) # array('i', [1, 20, 3, 4, 5])
Using .extend()
nums.extend([6, 7])
print(nums) # array('i', [1, 20, 3, 4, 5, 6, 7])
Explanation:
.append()adds a single value..extend()adds multiple values from an iterable.
Removing Elements
nums.remove(20)
print(nums) # array('i', [1, 3, 4, 5, 6, 7])
Explanation:
.remove(x)deletes the first occurrence ofx.
Using .pop()
nums.pop(2)
print(nums) # Removes item at index 2
Explanation:
.pop(index)removes the item at the given index and returns it.
Looping Through an Array
for num in nums:
print(num)
Explanation:
- Arrays are fully iterable using
forloops.
Reversing and Slicing Arrays
print(nums[::-1]) # Reversed array
print(nums[1:4]) # Slice from index 1 to 3
Explanation:
- Use slicing for sub-arrays or reversing.
Array Methods Summary
| Method | Description |
|---|---|
append(x) | Adds item x at the end |
extend(iter) | Adds multiple elements from iterable |
insert(i, x) | Inserts x at position i |
remove(x) | Removes first occurrence of x |
pop([i]) | Removes and returns item at index i |
index(x) | Returns index of x |
reverse() | Reverses the array in place |
buffer_info() | Returns memory address and element count |
Arrays vs Lists
| Feature | array.array() | list |
|---|---|---|
| Type Restriction | Fixed type | Mixed types allowed |
| Speed | Faster for numerics | Slower for large data |
| Memory Use | Efficient | Higher overhead |
| Use Case | Numeric computing | General use |
Best Practices
- Use arrays when storing large homogeneous numeric data.
- Select the correct type code to minimize memory.
- Use NumPy arrays for more complex numerical tasks (e.g., matrices, broadcasting).
- Avoid arrays for storing text, mixed data types, or objects—use lists or dictionaries instead.
Summary – Recap & Next Steps
Python’s array module provides a simple way to store efficient, fixed-type sequences. Though less common than lists, arrays are useful in performance-critical tasks involving large numeric data.
Key Takeaways:
- Arrays store fixed-type, homogeneous data.
- Use
.append(),.extend(),.remove(), and.pop()to manipulate arrays. - Use arrays instead of lists when speed and memory matter.
Real-World Relevance:
Used in IoT, signal processing, sensor data, and numerical simulations where performance matters.
FAQ Section – Python Arrays
What are type codes in arrays?
Type codes define the type of data in an array (e.g., 'i' for integers, 'f' for floats).
Are arrays faster than lists?
Yes. Arrays use less memory and are faster when working with large numeric datasets.
Can arrays store strings?
Yes, using type code 'u' for Unicode characters, but lists or strings are generally better for text.
What’s the difference between array.array() and NumPy?
array.array() is built-in and basic. NumPy arrays are more powerful for math-heavy tasks.
How do I convert a list to an array?
Use:
from array import array
arr = array('i', [1, 2, 3])
Share Now :
