https://docs.python.org/3/tutorial/stdlib2.html#tools-for-working-with-lists

array

  • 고정 타입(예: H = 2바이트 부호 없는 정수)만 담아 리스트 보다 메모리를 아낍니다.
from array import array
a = array('H', [4000, 10, 700, 22222])
print(sum(a))        # 26932
print(a[1:3])        # array('H', [10, 700])

collections.deque

  • 양쪽 끝에서 append/popleft가 빠른 리스트 대체품으로 큐 구현에 적합합니다.
from collections import deque
 
tasks = deque(['task1', 'task2'])
tasks.append('task3')
print(tasks.popleft())  # task1

bisect

  • 정렬된 리스트에 새 항목을 삽입할 위치를 찾아주므로 정렬 상태를 유지합니다.
import bisect
scores = [(100, 'perl'), (200, 'tcl'), (400, 'lua')]
bisect.insort(scores, (300, 'ruby'))
# [(100, 'perl'), (200, 'tcl'), (300, 'ruby'), (400,'lua')]

heapq

  • 리스트를 최소 힙으로 다뤄 가장 작은 값에 빠르게 접근합니다.
from heapq import heapify, heappush, heappop
 
data = [5, 7, 3]
heapify(data)              # 힙 구조로 재배치
heappush(data, 2)          # 새 값 추가
smallest = heappop(data)   # 최솟값 2

용도에 맞는 자료구조를 고르면 속도·메모리 측면에서 이득을 볼 수 있습니다.

← python 3.14으로