Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
Tips and Traps¶
A deque is implemented via the circular queue data structure and it has O(1) time complexity appending from both ends.
Unlike list and tuple collections, a deque CANNOT be sliced!
from collections import dequeq = deque()
q.appendleft(36)
q.appendleft(25)
q.appendleft(9)
qdeque([9, 25, 36])Unlike list and tuple collections, deque CANNOT be sliced!
q[1:]---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/workspace/blog/misc/content/2022/03/hands-on-the-deque-collection-in-python/hands-on-the-deque-collection-in-python.ipynb Cell 7' in <cell line: 1>()
----> <a href='vscode-notebook-cell://dclong-blog-nymhodme885.ws-us34.gitpod.io/workspace/blog/misc/content/2022/03/hands-on-the-deque-collection-in-python/hands-on-the-deque-collection-in-python.ipynb#ch0000007vscode-remote?line=0'>1</a> q[1:]
TypeError: sequence index must be integer, not 'slice'