Efficient Data Structures — array, deque, bytearray
Stryg for at vise menuen
Python's built-in list is a general-purpose container that can hold any mix of objects. That flexibility comes at a memory cost. When you know the type and access pattern of your data in advance, more specialized structures save memory and improve performance.
array.array — Typed Numeric Arrays
A Python list of integers stores each integer as a full Python object (28+ bytes each). array.array stores raw C-level values – a signed 32-bit integer takes exactly 4 bytes:
12345678910111213import sys import array # Comparing memory usage of list vs array for numeric data price_list = [100, 200, 150, 300, 250, 400, 175, 225] price_array = array.array("i", [100, 200, 150, 300, 250, 400, 175, 225]) # "i" = signed int print(f"list size: {sys.getsizeof(price_list)} bytes") print(f"array size: {sys.getsizeof(price_array)} bytes") # Accessing elements works the same way print(price_array[0]) # 100 print(price_array[2:5]) # array('i', [150, 300, 250])
Common type codes:
Use array.array when storing large sequences of uniform numeric data without NumPy.
collections.deque – Efficient Queue Operations
A list supports fast appends and pops from the right (O(1)), but inserting or removing from the left is O(n) – every element shifts. deque (double-ended queue) supports O(1) operations at both ends:
1234567891011121314151617181920from collections import deque import time # Comparing left-insert performance: list vs deque size = 100000 list_queue = [] start_time = time.time() for item_id in range(size): list_queue.insert(0, item_id) # O(n) – slow list_time = time.time() - start_time deque_queue = deque() start_time = time.time() for item_id in range(size): deque_queue.appendleft(item_id) # O(1) – fast deque_time = time.time() - start_time print(f"list insert(0): {list_time:.3f}s") print(f"deque appendleft: {deque_time:.3f}s")
deque also supports a maxlen parameter – when the deque is full, new items push out the oldest ones automatically. This is ideal for sliding windows and recent-event buffers:
12345678from collections import deque # Keeping only the last 5 sensor readings sensor_readings = deque(maxlen=5) for reading in [10, 20, 30, 40, 50, 60, 70]: sensor_readings.append(reading) print(list(sensor_readings))
bytearray — Mutable Binary Buffers
bytes is immutable – every modification creates a new object. bytearray is a mutable sequence of bytes, useful for building binary payloads, parsing protocols, or processing binary files in-place:
12345678910111213import sys # Building a binary data frame using bytearray header = bytearray(b"\x01\x02\x03") payload = bytearray(b"transaction_data") # Modifying in-place – no new object created header[0] = 0xFF print(header) # bytearray(b'\xff\x02\x03') # Concatenating efficiently frame = header + payload print(sys.getsizeof(frame))
Choosing the Right Structure
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat