Bounded Deque And Maxlen Attribute

The maxlen attribute of a bounded deque:

The maxlen attribute is the maximum length of a bounded deque. 

When a deque is created with the maxlen parameter specified it is called a bounded deque and such a deque can hold only upto the maxlen number of elements. Operations append(), appendleft(), extend() and extendleft() will not raise any exception when new elements are added after the deque reaches its maximum length. Instead, when a new element is added to a bounded deque after it has reached its maximum length, an element from the other end of the deque is discarded to accommodate the new element. However, as per Python version 3.11.1, when an element is added to a bounded deque when the deque has already reached its maximum size, the insert() method will raise an IndexError stating that “the deque already at its maximum size”.


# Example Python program that creates a 
# bounded deque
import collections

# Create a bounded deque
dLength = 5
d = collections.deque(maxlen=dLength)

# Append elements beyond the maximum length
for i in range(0, 7):

# Pop all the elements from the deque
# and print them
for i in range(0, d.maxlen):








Copyright 2023 ©