What is deque in Python?

The Python Deque module (known as a double-ended queue) is part of the built-in library known as collections. Deque contains methods for adding and removing items from either end of the queue and can create a queue from any iterable. Queues are similar to lists, except you can add and remove items either at the beginning (left) or the end (right). Deque is preferred over the list when you need faster add, get, and delete operations at both ends of the container. The deque module provides O(1) time complexity for add and get operations compared to a list, which provides O(n) time complexity. In this Python Deque Example, we use the deque.append() method to add an item to the end of the deque. Click Execute to run the Python Deque Example online and see the result.
What is deque in Python? Execute
from collections import deque 
      
a = deque(['name','surname','gender'])  
a.append('age')

print(a)
Updated: Viewed: 924 times

What is Deque?

Deque (Doubly Ended Queue) in Python allows elements to be added and removed from either end. The Deque module is a part of the collections library. A deque is preferable over a list whenever we need to perform append and pop operations quickly from both ends of a container, as deque implements append and pop operations with an O(1) time complexity as compared to a list, which has an O(n) time complexity.

How to use Deque in Python?

To use a deque in your Python code, you need to import the "collections" library first:

Python Deque Syntax
from collections import deque

Methods for modifying a deque

The following is a brief description of the methods used to modify the deque:

Method append()

The append() is used to append the data item in its parameter to the right end of the deque.

Python append() Example
from collections import deque 
      
a = deque(['1','2','3'])  
a.append('4')

print(a)

# deque(['1', '2', '3', '4'])

Method appendleft()

The appendleft() is used to add the data item in its parameter to the left end of the deque.

Python appendleft() Example
from collections import deque 
      
a = deque(['1','2','3'])  
a.appendleft('4')

print(a)

# deque(['4', '1', '2', '3'])

Method insert(index, value)

The insert() is used to insert the value described in the "x" parameter into the index number "i" specified in the parameters.

Python insert() Example
from collections import deque 
      
a = deque([2, 2, 3, 3, 4, 2, 1])
a.insert(3,4)

print(a)

# deque([2, 2, 3, 4, 3, 4, 2, 1])

Method extend(iterable)

The extend() is used to insert multiple data items into the right end of a deque. The passed parameter is repeated.

Python extend() Example
from collections import deque 
      
a = deque([1, 2, 3])
a.extend([4,5,6])

print(a)

# deque([1, 2, 3, 4, 5, 6])

Method extendleft(iterable)

The extendleft() is used to insert multiple items of data into the left end of a deque. The passed parameter is repeated. The order is also reversed as a result of left additions.

Python extendleft() Example
from collections import deque 
      
a = deque([3, 2, 1])
a.extendleft([4,5,6])

print(a)

# deque([6, 5, 4, 3, 2, 1])

Method pop()

The pop() is used to remove a data item from the right end of the deque.

Python pop() Example
from collections import deque 
      
a = deque(['1','2','3'])  
a.pop()

print(a)

# deque(['1', '2'])

Method popleft()

The popleft() method removes an item from the right end of a deque.

Python popleft() Example
from collections import deque 
      
a = deque(['1','2','3'])  
a.popleft()

print(a)

# deque(['2', '3'])

Method remove()

The remove() method removes the first occurrence of the value specified in the parameters.

Python remove() Example
from collections import deque 
      
a = deque([2, 2, 3, 3, 4, 2, 1])
a.remove(3)

print(a)

# deque([2, 2, 3, 4, 2, 1])

Method count()

The count() method counts the total number of occurrences of the value specified in the parameters.

Python count() Example
from collections import deque 
      
a = deque([2, 2, 3, 3, 4, 2, 1])
a.count(2)

print(a)

# 2

Method index(element, begin, end)

The index() returns the first index value specified in the parameters and starts the search from the beginning to the end of the index.

Python index() Example
from collections import deque 
      
a = deque([2, 2, 3, 3, 4, 2, 1])
a.index(4,3,5)

print(a)

# 4

Method rotate()

The rotate() is used to rotate the deque by the number specified in the parameters. If the set number is negative, the rotation is to the left. The rest of the rotation is to the right.

Python rotate() Example
from collections import deque 
      
a = deque([1, 2, 3])
a.rotate(-1)

print(a)

# deque([2, 3, 1])

Method reverse()

The reverse() is used to reverse the order of the deque data items.

Python reverse() Example
from collections import deque 
      
a = deque([1, 2, 3, 4, 5])
a.reverse()

print(a)

# deque([5, 4, 3, 2, 1])

See also