
Python deque
Python deque is similar to list and has had many useful methods since Python 3.5.
from collections import deque
d = deque()
d.append(1)
d.append(2)
print(d) # deque([1, 2])
The deque object can be handled like a list.
from collections import deque
d = deque()
d.append(1)
d.append(2)
d.append(3)
d.reverse()
print(d) # deque([3, 2, 1])
d.clear()
print(d) # deque([])
Insert an object at the first position
from collections import deque
d = deque([1, 2, 3])
print(d) # deque([1, 2, 3])
d.appendleft(5)
print(d) # deque([5, 1, 2, 3])
The appendleft()
is a very useful method of deque. It inserts 5
at the first position and all the indexes move. This kind of updating costs the memory in list.
Comments
Powered by Markdown