deque remove item by index

Denly picture Denly · May 25, 2018 · Viewed 8.4k times · Source

Is there any way to remove an item in deque by index?

dq = deque(['a','b','c'])
dq.removeByIndex(1) 
#output deque(['b', 'c'])

I only see remove by value in the doc. Also, I know I can just pop it i times and then put it back, but it doesn't look pretty.

deque

Answer

user5777975 picture user5777975 · May 25, 2018

You can try this :

from collections import deque
deq = deque([1, 2, 3, 4])

del deq[1]
print(deq)

Output:

deque([1, 3, 4])