Using deque for Efficient Queues and Stacks in Python


Use deque from collections for efficient appends and pops from both ends.

Source Code

from collections import deque
dq = deque([1, 2, 3])
dq.appendleft(0)  # Add to the front
dq.append(4)      # Add to the back
print(dq)
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments