deque
Anonymous contributor
Published Jun 30, 2024
Contribute to Docs
The deque, short for double-ended queue, is a Python data structure that efficiently adds and removes elements from both ends. It is a component of the collections module and serves as an alternative to the list for scenarios where frequent insertions and deletions occur at both ends. Deques are notably advantageous when a queue is needed to enable fast appends and pops from both ends or when a stack is required to support the same operations efficiently.
Syntax
from collections import deque
d = deque([iterable[, maxlen]])
iterable
: An optional parameter representing an iterable object (like a list, tuple, or string) used to initialize the deque. If no iterable is provided, an empty deque is created.maxlen
: An optional parameter that specifies the maximum length of the deque.
Example
The following example demonstrates the usage of deque
:
from collections import deque# Create a deque using a tuple of integersa = deque((8, 7, 9, 6))print(a)# Create a deque using a list of integersb = deque([45, 845, 65])print(b)# Create a deque using a range of integers from 5 to 9c = deque(range(5, 10))print(c)# Create a deque using a string, which will be split into individual charactersd = deque("wxyz")print(d)# Create a dictionary with some key-value pairsnumbers = {"firstname": "John", "age":25}# Create a deque containing the keys of the dictionarye = deque(numbers.keys())print(e)# Create a deque containing the values of the dictionaryf = deque(numbers.values())print(f)# Create a deque containing the items (key-value pairs) of the dictionaryg = deque(numbers.items())print(g)
The above code produces the following output:
deque([8, 7, 9, 6])
deque([45, 845, 65])
deque([5, 6, 7, 8, 9])
deque(['w', 'x', 'y', 'z'])
deque(['firstname', 'age'])
deque(['John', 25])
deque([('firstname', 'John'), ('age', 25)])
Equivalent Methods for Stacks and Queues
Deques can be used to implement both stacks and queues efficiently:
- Stacks: A stack operates on the Last In, First Out (LIFO) principle, where pushing (adding an item) is done with
.append()
to the right end, and popping (removing the most recent item) is performed using.pop()
from the right end. - Queues: A queue, based on the First In, First Out (FIFO) principle, utilizes
.append()
at the right end for adding (enqueueing) and.popleft()
from the left end for removing (dequeuing).
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours