Learning memo / memorandum
The last-in first-out (LIFO) type buffer is ** stack **, and the first-in first-out (FIFO) type buffer is ** queue **.
stack = []
for i in range(10):
stack.append(i)
if len(stack)>4:
print(stack.pop())
Execution result
4 5 6 7 8 9
queue = []
for i in range(10):
queue.append(i)
if len(queue)>4:
print(queue.pop(0))
Execution result
0 1 2 3 4 5
However, implementing a queue as a list is inefficient because after the first data is retrieved by pop (0), the rest of the data must be shifted one by one, so use the deque in the collections package. Use.
from collections import deque
queue = deque([])
for i in range(10):
queue.append(i)
if len(queue)>4:
print(queue.popleft())
Execution result
0 1 2 3 4 5
Recommended Posts