π 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
array
module 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
array
module - 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
array
module 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
for
loops.
π§ͺ 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 :