This wiki.python.org page on algorithmic complexity of some data structures says the following for a collections.deque
object:
A deque (double-ended queue) is represented internally as a doubly linked list. (Well, a list of arrays rather than objects, for greater efficiency.) Both ends are accessible, but even looking at the middle is slow, and adding to or removing from the middle is slower still.
Two questions:
1) Is adding to the middle of a deque
even possible? I don't see any method to do so in the API.
2) Why would removing (or adding) be slower than lookup in the middle of a deque
? It's a doubly-linked list, so add/remove should be a constant time operation once you've found the object you want to add.