The Deque module is an item in the Python standard library collections, which provides a sequence that can be manipulated at both ends, which means that you can perform add or delete operations before and after the sequence.
1. Create the deque sequence:
from Import dequed=deque ()
The 2.deque provides a list-like method of operation:
d=deque () d.append (3) d.append (8) d.append (1)
So at this point
D=deque ([3,8,1]), Len (d) =3,d[0]=3,d[-1]=1
3. Use pop on both ends:
D=deque (' 12345 ')
So
D=deque (['1'2"3" 4"5"])
D.pop () throws the ' 5 ', D.leftpop () throws the ' 1 ', and the Visible default pop () throws the last element.
4. Limit the length of the deque
D=deque (maxlen=20) for in range: d.append (str (i))
At this point, the value of D is
D=deque (['Ten',' One',' A',' -',' -',' the',' -',' -',' -',' +',' -',' +',' A',' at',' -',' -',' -',' -',' -',' in'], maxlen=20)
, it is visible that items on the other side are automatically deleted when the limit length of deque increases by more than the limit number of items.
5. Add list items to deque:
D=deque ([1,2,3,4,5]) d.extend ([0])
So at this point
D=deque ([1,2,3,4,5,0])
D.extendleft ([6,7,8])
At this time
D=deque ([8, 7, 6, 1, 2, 3, 4, 5, 0])
Detailed description of deque modules in Python