Bounded Deques: End Retention and Full-Capacity Insertion
2026-11-21

Bounded Deques: End Retention and Full-Capacity Insertion

Bounded deques retain at most their configured number of elements, but their full-capacity behavior depends on the operation. Appending at an end discards from the opposite end, while inserting into a full bounded deque raises an error. Those are explicit retention rules, rather than a promise that every attempted addition succeeds in the same way.

An append can replace the oldest element at the other end

The Python collections documentation defines deque with an optional maxlen. Use a fictional deque holding A, B and C from left to right, with maximum length three. Appending D on the right gives B, C, D: A leaves from the left.

The returned container still contains three elements. It did not temporarily become a permanently four-element queue, nor did the new D get rejected merely because the container was full. The configured rule accepts the incoming end element and removes an element at the opposite end.

These invented labels describe in-memory positions only. Removing A from this container does not mean a task named A was completed, acknowledged or archived elsewhere. Retention state and external work completion are separate concepts, even when an application calls the structure a queue.

ABCappendDbecomesBCDthenappendleftXbecomesXBCwithmaxlen3.
The trace tracks this container's retained labels, not work completion or object lifetime. Source: Zhilianhai editorial desk, original AI-assisted conceptual illustration.

A left append discards from the right instead

Continue from B, C, D and append X on the left. The result becomes X, B, C, with D discarded from the right. The direction of addition determines which end loses an element. It is therefore more precise to name append or appendleft than simply say that an item was added.

The original CPython implementation has separate end-append helpers. The right helper checks whether trimming is needed and removes from the left; the left helper performs the symmetric removal from the right. These inspected helpers support the example's direction rule.

When the deque is not full, an append increases its current length without discarding an existing element. Maxlen is a ceiling, while len reports the current count. A partially filled container and a full container can share the same configured ceiling but respond differently to the next append.

Insert has a different full-capacity contract

Start another full three-element deque A, B, C. Trying to insert X at an index raises IndexError because the insertion would exceed maxlen. It does not silently discard an element to make room, even though append and appendleft have that behavior at their respective ends.

The inspected insert implementation checks the full-size condition before dispatching to end insertion or rotating for a middle insertion. An insertion at an extreme index therefore does not provide a workaround that automatically inherits an append's discarding policy when the deque is full.

AppendDretainsBCDwhileinsertXat1raisesIndexErrorandkeepsABC.
Full-capacity insertion is not an automatic opposite-end discard. Source: Zhilianhai editorial desk, original AI-assisted conceptual illustration.

The local check confirms that the rejected insertion leaves the fictional contents unchanged. This illustrates the documented full-capacity error, without claiming rollback for every unrelated container operation or for arbitrary exceptions raised by user objects.

Repeated left appends reverse the supplied iteration order

Extendleft processes its input by repeated left appends. With a starting deque A, B, maximum three, and incoming labels P then Q, the intermediate state is P, A, B. The next left append produces Q, P, A and drops B from the right.

The final leading labels are Q, P, rather than P, Q. That reversal follows the sequence of end operations. It does not mean the input iterable itself was sorted or reversed in place. Recording the incoming order and each end update explains both the retained values and their positions.

Construction likewise consumes an input sequence from left to right. Giving A through E with maximum three retains C, D, E. The initial input length and the resulting retained length are different quantities under a bounded retention rule.

The configured bound is distinct from the retained objects

The maxlen attribute is read-only. Removing elements can create room, but does not change that bound. An unbounded deque uses a different setting, so an example should state its actual configuration instead of treating every deque as a fixed-capacity container. A useful trace also records discarded labels explicitly, because the retained sequence alone does not reconstruct earlier contents.

Stored values remain object references. Discarding a reference from the deque does not establish that no other part of a program retains the referenced object. The example concerns this container's visible contents and makes no claim about resource lifetime or processing success.

All checks are sequential and use synthetic labels, without operating a production queue. Preserve the bound, current contents and exact operation when describing an update. Related explanations appear in the container methods section and the technical article archive.

18028484626 CONTACT US

公司:智联海(惠州)科技有限公司

地址:惠州仲恺高新区陈江街道白云山工业大道 5 号六楼 601

Q Q:18028484626

Copyright © 2002-2026

粤xxx

咨询 在线留言 电话:18028484626
微信 微信扫码添加我