ML Foundations
Maintain a total when a bounded deque evicts a value
Read the outgoing value before appending to a full bounded deque so a rolling total remains aligned.

Evidence and testing
Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.
A bounded window discards from the other end
A bounded collections.deque keeps at most maxlen items. When a full deque receives an item on the right using append, it discards an item from the left. If a separate running total is kept, the caller must account for the outgoing value as well as the incoming one. [1]
A deque can be useful for a fixed recent window, but automatic removal does not update separate application state. This example tracks an integer total alongside the deque and checks it against a fresh sum. [1, 2]
Capture the outgoing item before append
Run python examples/python_deque_window_sum.py. The full window starts with 2, 4 and 6. The script reads its leftmost value before appending 8, then subtracts that outgoing value and adds the incoming value. The resulting window is 4, 6 and 8 and the maintained total is 18. [1]
The script compares the maintained result with sum(window) and asserts equality. The conditional outgoing value of zero handles a window that is not yet full in this positive-capacity pattern; the shown fixture exercises the full-window case. [2]
Executed with Python 3.12.14; stdout and checksum recorded in private evidence.
from collections import deque
window = deque([2, 4, 6], maxlen=3)
total = sum(window)
incoming = 8
removed = window[0] if len(window) == window.maxlen else 0
window.append(incoming)
total += incoming - removed
print('removed:', removed)
print('window:', list(window))
print('maintained total:', total)
print('recomputed total:', sum(window))
assert total == sum(window)
Recorded output
removed: 2 window: [4, 6, 8] maintained total: 18 recomputed total: 18
Define the window contract
This pattern assumes a positive maxlen, right-side append and integer values. Appending on the left discards from the right instead, so the outgoing side must change. A zero-length window needs separate handling. [1]
For floating-point inputs, repeated addition and subtraction can accumulate rounding error; choose a suitable numerical policy and check it for the application. The example makes no timing or thread-coordination claim.
References: [1]