Home » Python Tutorial » Python Deque with Syntax and Examples

Python Deque with Syntax and Examples

Python deque stands for double-ended queues. Deque is generally pronounced as “deck” in normal conversation.

Python deque is iterables which allows adding and deleting elements from either side. Python deque can be a substitute for sequence containers such as list, dictionaries in python. Deques are imported from modules known as “collections” in python.

Python deque Syntax

collections.deque([iterable[, maxlen]])


Import collections


(['p', 'r' , 'a', 'd')]

Note: Other sequence containers present in the collection module are ChainMap, UsrDict, UserString, etc.

Time complexity

The time complexity of inserting or deleting items from either end of python deque is the same O(1), unlike list which ensures O(n).

Table of Contents

Deque Python Operations

There are multiple operations that can be performed on deque in python. These operations manipulate the values in python deque in a different manner.

1. Insertion

Insertion operation is used to insert values at a specified position. It may be at the start position,end position, or at the position indicated by the index.Inserting elements into deque in python is also known as “Populating”.

Many functions can be used to insert values in deque in python.

1. append() – The function append() is used to insert values at right side end.It adds value at last in deque.

2.happen left() – The function happens left() is used to insert values at the left end of deque in python.

3.insert(i,a) – The function insert() is used to insert value ‘a’ at specified position by ‘i’.

4.extend(list) –  This function always inserts multiple values together by storing new values in the list as an argument.

5.extend left(list) – This function inserts multiple values together at the left end of the deque by storing new values in the list as an argument.

Insertion Example

import collections


deque([5, 6, 7, 8])
deque([5, 6, 7, 8, 9])
deque([4, 5, 6, 7, 8, 9])
deque([4, 5, 6, 7, 8, 9, 10])
deque([4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])

2. Deletion

Deletion in deque can be performed by deleting the item at any end of the deque or any specified position in the deque. The Deletion of items in deque in python can be performed by pop() and popleft(). Elements in deque from both ends can be deleted at the same time using separate threads. The reason is that deque in python is “thread-safe in nature”.It means that the user have to not perform thread locking. It can be conducted by python deques easily. Deleting elements in python is also known as ‘consuming”.

Deletion Example

import collections
print("deleted item is:",a.pop())
print("deleted item is:",a.popleft())


deleted item is: 8
deleted item is: 5

3. clear()

This function is used to remove all the items from the list. It leaves nothing on the list.

clear() Example

import collections



4. Len()

This function len() is used to find the length of deque in python.It gives the length of the selected deque in output.

5. rotate()

It helps to rotate elements in deque.A positive number in the argument rotates the element on the right side of the deque. The negative number rotates the element at the left side of the deque in python.

Rotate() Example:

import collections


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

6. reverse()

This is used to reverse the order of deque in python.

Reverse() Example

import collections


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


How to use deque in python?

deque(double-ended queue) in python is used by following few steps. First of all import collection module by writing syntax “import collections” which contain deque as a sequence container. Then storing value or multiple values in the deque. Now the deque can be used for multiple operations such as append, pop, reverse, rotation, etc.

What is deque in python?

Deque(Double-end queue) in python are iterables or sequence containers. These are imported from collection modules. These deques are used to store or delete items from any side right or left. Deque supports O(1) time complexity in the insertion and deletion of elements. Deques are faster in adding or deleting elements at the start or end of the deque object in python

How to create deque in python?

Deque in python can easily be created after importing deque from collections.Then storing appropriate values in deque by following syntax var=collection.deque([values]).We can too add elements aftermath of the creation of deque by using methods such as append() or extend().

How to check if deque is empty in python?

It can be checked easily by converting python deque object as a bool value. Appropriate code will return “true” if the list is empty or “false” if the list is not empty. In another way, we can also use the lens method to find the size of the deque. If it returns 0 then there are no elements in the deque. If it returns a value other than 0, that would be its length

Did you know?
1. Polymorphism in Python
2. Python return
3. Python Strftime
4. Encapsulation in Python
5. Operator Overloading in Python
6. iloc in Python
7. Python Pop
8. Python Enum
9. Python Pass
10. Python deque

Pin It on Pinterest