Python Forum

Full Version: If you deque a list, can it still be indexed?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I was wondering if when you deque a list, can you still access the middle of the list using the index?

For example :

from collections import deque
q = deque("David", "Sally", "Ralph", "Lisa")

I know I can POPLEFT, POP and append to the list, but am I able to also remove items that are not first or last using indexing?
Have you tried it? If so, what was the outcome?
I tried it. It works.
new to me so i went to the docs in a hurry.  the term "list-like" makes me think middle indexing should just work; maybe not as fast as the right and left ends but eventually. is there a reason to think they would not work?
The module is just for that. Fast indexing, fast adding elements to both sides.
(Nov-07-2016, 11:25 AM)wavic Wrote: [ -> ]Fast indexing, fast adding elements to both sides.
Those aren't the same thing. A deque is a doubly linked list allowing constant time insertion and extraction at the beginning or end. But getting an element by index is a linear time operation as you need to iterate through links to get to it.

From here https://wiki.python.org/moin/TimeComplexity:
Quote: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.