4

Is there any implementation of such a queue in Python which has a fixed length, and when full, pops the left-most element while appending one to the right?

Suppose q = Queue([1,2,3,4,5]) is my queue with a maximum length of 5, and I say q.append(6), then expected output for print(q) should be Queue([2,3,4,5,6])

This question could also be linked to: Is there a fixed sized queue which removes excessive elements?

pissall
  • 7,109
  • 2
  • 25
  • 45

1 Answers1

6

Answering my own question:

I have tried using collections.deque() and queue.Queue(), and deque is such an implementation

d = deque(maxlen=5)
d.extend([1,2,3,4,5])
print(d)
# deque([1, 2, 3, 4, 5], maxlen=5)
d.append(10)
print(d)
# deque([2, 3, 4, 5, 10], maxlen=5)
pissall
  • 7,109
  • 2
  • 25
  • 45