Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Efficient Data Structures — array, deque, bytearray | Writing Memory-Efficient Code
Python Memory Management

Efficient Data Structures — array, deque, bytearray

Deslize para mostrar o menu

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:

12345678910111213
import 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:

1234567891011121314151617181920
from 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:

12345678
from 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:

12345678910111213
import 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

question mark

Why is list.insert(0, item) significantly slower than deque.appendleft(item) for large sequences?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 3

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Seção 2. Capítulo 3
some-alt