![]() |
If you deque a list, can it still be indexed? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: If you deque a list, can it still be indexed? (/thread-802.html) |
If you deque a list, can it still be indexed? - netrate - Nov-06-2016 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? RE: If you deque a list, can it still be indexed? - sparkz_alot - Nov-07-2016 Have you tried it? If so, what was the outcome? RE: If you deque a list, can it still be indexed? - ichabod801 - Nov-07-2016 I tried it. It works. RE: If you deque a list, can it still be indexed? - Skaperen - Nov-07-2016 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? RE: If you deque a list, can it still be indexed? - wavic - Nov-07-2016 The module is just for that. Fast indexing, fast adding elements to both sides. RE: If you deque a list, can it still be indexed? - Mekire - Nov-07-2016 (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. RE: If you deque a list, can it still be indexed? - Larz60+ - Nov-07-2016 Some good examples: https://pymotw.com/3/collections/deque.html |