Python Forum
If you deque a list, can it still be indexed?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If you deque a list, can it still be indexed?
#1
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?
Reply
#2
Have you tried it? If so, what was the outcome?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
I tried it. It works.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
The module is just for that. Fast indexing, fast adding elements to both sides.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(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.
Reply
#7
Some good examples: https://pymotw.com/3/collections/deque.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Deque to string in Python 3 anthares 4 4,613 Jun-11-2020, 06:18 AM
Last Post: anthares
  Indexed position editing of a lst ShruthiLS 4 2,485 Sep-26-2019, 09:23 AM
Last Post: perfringo
  Operations on indexed variables in loop Fibulavie 1 1,929 Aug-14-2019, 06:07 AM
Last Post: fishhook

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020