I am having trouble understanding how the deque works in the snippet of code below, while trying to recreate a queue and a stack in Python.
Stack Example - Understood
stack = ["a", "b", "c"]
# push operation
stack.append("e")
print(stack)
# pop operation
stack.pop()
print(stack)
As expected when pushing and popping, the "e" goes Last In, First Out (LIFO). My question is with the example below.
Queue Example - Not Understanding
from collections import deque
dq = deque(['a','b','c'])
print(dq)
# push
dq.append('e')
print(dq)
# pop
dq.pop()
print(dq)
When pushing and popping, the "e" goes Last In, First Out (LIFO). Shouldn't it be First In, First Out (FIFO)?
deque
documentation. You might be looking fordq.popleft()
(or.appendleft()
). In either case, the "de" in dequeue stands for "double-ended". – Joe