Deque in Python

October 15, 2023
Justin Lumiere

deque python

Deque is a Python class that implements the queue abstract data type. This class allows you to insert and remove items from both ends of a queue by using dedicated append() and pop() functions, with additional capabilities including extend(), rotate(n), and reverse() functions for further assistance.

Contrary to lists, which require looping for random access, deque offers constant time complexity for appending and popping operations; making it both thread-safe and memory efficient.

Queues and Stacks

Deques can be used for various tasks, including queues and stacks. Deques provide an effective solution for maintaining undo/redo history, enqueue requests at web services, safely exchanging data between threads, as well as safely exchanging between threads. In this article, you'll discover some of the more frequently utilized uses for deques as an extremely versatile Python data type.

Deques differ from lists by offering fast append and pop operations; their performance scales proportionally with the number of items within it, since these don't require memory reallocation; in contrast, list operations require reallocation that typically takes O(n).

Deque also supports indexing and rotation; its rotate() function turns elements of a deque by an integer value or, if specified, by int value. Furthermore, it features clear() which removes all items from its deque; similar to empty() of lists but without raising an error if deque isn't empty like clear() does (whereas remove() does! ).

Deque data structures are double linked lists with both a head and tail nodes that implement First-In, First-Out (FIFO) and Last-In, First-Out (LIFO) orderings of queues or stacks, supporting item insertion/deletion at either end - known as front or rear ends of deques respectively.


Deque is a versatile Python data structure that lets you work with queues and stacks easily, offering support for various operations like pop(), extend() and clear() as well as utility functions for tasks like counting indexing membership testing.

Deques differ from other collections in that they can be iterated over using for loops, providing users with greater flexibility when working with it. Their implementation can be found within the collections package; and this class is thread-safe so multiple threads can safely add and remove items simultaneously from either end without risking data corruption or other issues.

The deque class implements most of the methods and operations found in sequences and mutable sequences, with direct slicing not supported; to achieve this functionality, please use itertools' islice() function instead.

The deque class provides several useful functions, including index(e, start, end), count(value), rotate(n) and count(value). These functions allow users to quickly search the deque for specific values by providing index(e, start, end), index(e, start, end) and count(value) functions that return their first appearance in the deque. count(value) counts the number of values within it that match up with given values - especially useful when dealing with large collections. Furthermore, rotate(n) can be used to rotate this deque by n steps!


Deque data structure is an advanced container class with thread-safe, memory efficient append and pop operations on both ends, which makes it useful for creating queues and stacks - two widely-used abstract data structures in computer programming.

Deques can be limited in their length by specifying a maximumlen argument when creating them, which prevents it from growing too large. When filled, adding to one end will automatically discard items at both ends - saving both time and space when inserting items at both ends!

Bounded deques can also be useful for tracking transactions and data pools where only recent information is of use, for instance a website could use one to keep track of which websites a user visited over time, using it to avoid unnecessary storage and processing of unnecessary data.

Like lists, bounded deques can be modified using insert(element, begin, end), remove(element) and clear() functions to add and remove elements respectively. Insertion will cause it to exceed its maxlen limit; an IndexError would then be raised when inserting data that exceeds this number. Furthermore, there are other handy functions such as clear() and copy() which allow fast access to their contents.

Inserting & removing items

Deques (double-ended queue) in Python provide a generalized version of stacks and queues. Their implementation as doubly linked lists allow fast and efficient operations when adding or removing items from either end, with operations taking only O(1) time per insertion/removal action. Deque's flexibility also makes it ideal for use with unordered iterables such as sets that do not have fixed ordering rules.

To insert values into a deque, call the insert() function and specify an index number as part of its parameters. Deque also provides various other methods for inserting and removing items, including extend(), extendleft(), reverse() and rotate().

IndexError will be raised when an inserted item causes a deque to expand beyond its maxlen limit. To delete an element from a deque, either use pop() or popleft(); both methods will remove the first element that matches your specified value from its position in the deque.

To rotate a deque, use the.rotate() function. If your parameter n is positive, it will rotate elements rightward by one position; otherwise it will rotate them leftward - much like how an ordinal rotates on a clock face. If you need to stop any existing rotation altogether, use clear() as well.

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram