How to use deque in Python

What is deque structure and how to use deque

Double-ended queues, or deques, are used to remove elements in the order in which they were added.

A deque is created from an iterable object and has several useful methods:


from collections import deque 
q = deque(range(5)) #   w  w  w. j  av a2 s . co  m
q.append(5) 
q.appendleft(6) 
print q 
print q.pop() 
print q.popleft() 
q.rotate(3) 
print q 
q.rotate(-1) 
print q 

The code above generates the following result.





















Home »
  Python »
    Language Basics »




Python Basics
Operator
Statements
Function Definition
Class
Buildin Functions
Buildin Modules