🌐
Python
docs.python.org › 3 › library › collections.html
collections — Container datatypes
Starting in version 3.5, deques support __add__(), __mul__(), and __imul__(). ... >>> from collections import deque >>> d = deque('ghi') # make a new deque with three items >>> for elem in d: # iterate over the deque's elements ...
Discussions

Hello, Guys. I am trying to understand the queue and deque in Python.

Why are you using version 2.5? That’s pretty out of date.

More on reddit.com
🌐 r/learnprogramming
2
1
June 15, 2022
Can someone help me understand what collections.deque() in this example code does?

You're familiar with lists? It functions very similarly to a list. Like you can read from the documentaion, there's some optimization reason why the data structure is selected to be a deque instead of list, but from usage perspective they're quite identical.

An empty list vs. deque would be:

a_list = []
a_deque = collections.deque()

Initialized to some starting values instead of being empty:

a_list = [1,2,3]
a_deque = collections.deque([1,2,3])

And a way to initialize same value several times is to say [a_value]*n for example [1]*3 would be same as [1,1,1]. So finally the differences for that type of initialization:

a_list = [0.0]*buffer_size
a_deque = collections.deque([0.0]*buffer_size)

And then the only last difference to the actual code is that they also set a maximum length of the deque object so it can't ever hold more values than that.

More on reddit.com
🌐 r/learnpython
2
1
May 6, 2019
Python avoid deque import
Ig pop(0) from a list has O(n) time complexity whereas in deque it's O(1) approx More on reddit.com
🌐 r/leetcode
9
0
April 19, 2024
Can anyone tell what is the Collections module? In a way that a beginner can understand? I cannot understand why is it used and when is it used?
Yes. So first off, make sure you know what a collection is. You've probably used most of the basic ones. Forget the collections module for now. Collections are containers. They are well named because they "collect" multiple items together. The most basic containers don't need to be imported: list, tuple, set and dict. Let's look at list: # typically you would create a list like this: l_1 = [] l_1.append(4) # but this is also a perfectly acceptable way to create a list: l_2 = list() l_2.append(1) You see how that second way sort of looks like how you'd create any other object from it's class? The collections module is filled with specialised collections. Most of them build on those basic containers but add extra functions or behaviours that might be useful for you. Some of them are less useful in modern versions of python than they were in older versions of python. I'd say you probably don't need to know the collections module as a beginner. Most beginner stuff can be achieved with the standard collections. The collections in collections are quite well named, and if you can't immediately see what they might be used for, then that is okay - you don't have to use them! Using them might actually be over complicating things for now. Let's start with collections.OrderedDict, which is an extension on the standard dict. In old python (below 3.7), the order in which you add keys to the dict is NOT necessarily the order they are stored in, so when you loop through my_dict.items(), the keys could be in a different order every time. If you used an OrderedDict, the order in which you add keys is preserved. You don't have to worry about this one so much if you're using 3.7 or above - because standard dicts preserve the order of keys after Python3.7. (There are actually some subtle differences between OrderedDict and standard dicts, but they are not so important). There is no collections.UnorderedDict btw. collections.namedtuple is like a tuple that uses names to access it's inner variables, instead of numbers. It's not too dissimilar to what defining a class does, except you can't define methods for a namedtuple. Lets say you wanted to create a class that basically acts like a dict, or a string, or a list, but has some custom functionality, perhaps a new method like you wanted to add. In old python, you can't inherit from list, you had to inherit from UserList. There are still reasons to inherit from UserList in new python instead of lists, but they are beyond beginner. A lot of this module is beyond beginner. collections.Counter is an extension on the standard dict that will count up identical keys for you. collections.deque is a double ended queue. This is a very standard data structure in computer science. Queues are very much like a queue in real life, the first item in is the last item to come out. You might not need to use it so much in python just yet. The official python docs look scary, but if you read them in detail you will get a more complete picture of what these are used for, and what capabilities they have. https://docs.python.org/3/library/collections.html More on reddit.com
🌐 r/pythontips
4
19
August 10, 2023
People also ask

Is the collection module a necessary topic for Python?
Yes, the collection module is a necessary topic while learning Python. Counting objects, constructing queues and stacks, managing missing keys in dictionaries, and more are all possible with Python's collections module. Collections' data types and classes are created to be efficient and Pythonic. They are pretty valuable for your Python programming career, so taking the time to learn about this collection module is well worth your time and effort. The containers in the Collections module may be quite beneficial for business-level projects and models, adding significantly to the usefulness of g
🌐
upgrad.com
upgrad.com › home › blog › data science › python collections module: counter, chainmap, deque & tuple
Python Collections Module: Counter, ChainMap, Deque & Tuple | upGrad ...
What are the data types present in the collection Module?
Several data types are present in the collection module, such as deque, defaultdict, namedtuple, OrderedDict, Counter, ChainMap, UserDict, UserList, UserString, etc. These data types can be used for various reasons, such as adding and removing items from either end of the sequence, constructing default values for missing keys, and automatically adding them to the dictionary. These data types can also help by providing named fields that allow accessing items by name while keeping the ability to access items by index, counting unique items in a sequence or iterable, and treating several mappings
🌐
upgrad.com
upgrad.com › home › blog › data science › python collections module: counter, chainmap, deque & tuple
Python Collections Module: Counter, ChainMap, Deque & Tuple | upGrad ...
What is a collection module, and how is it useful?
Python's collection module supports several types of containers. A Container is an object used to store various items and give the means to retrieve and iterate over the enclosed objects. Tuple, List, Dictionary, and more built-in containers are available. Also, the Collections module has highly specialized and efficient container data types such as namedtuple(), deque, OrderedDict, counter, and so on that are far superior to standard Python containers.
🌐
upgrad.com
upgrad.com › home › blog › data science › python collections module: counter, chainmap, deque & tuple
Python Collections Module: Counter, ChainMap, Deque & Tuple | upGrad ...
🌐
ActiveState
code.activestate.com › recipes › 259179-deque-collection-class
deque collection class « Python recipes « ActiveState Code
January 12, 2004 - See documentation at: http://www.python.org/dev/doc/devel/lib/module-collections.html <br> Uses a dictionary as the underlying data structure for the deque (pronounced "deck", short for "double-ended queue", a generalization of stacks and queues) which provides O(1) performance for appends and pops from either end.
🌐
Pythontic
pythontic.com › containers › deque › introduction
The deque in Python | Pythontic.com
The deque is a container class in Python which can hold a collection of Python objects.
🌐
GeeksforGeeks
geeksforgeeks.org › python › deque-in-python
Deque in Python - GeeksforGeeks
A deque stands for Double-Ended Queue. It is a special type of data structure that allows to add and remove elements from both ends efficiently. This makes it useful in applications like task scheduling, sliding window problems and real-time ...
Published   2 weeks ago
🌐
GeeksforGeeks
geeksforgeeks.org › python › implementation-of-deque-using-list-in-python
Implementation of Deque Using List in Python - GeeksforGeeks
July 23, 2025 - A Deque (double-ended queue) is a data structure that allows insertion and deletion from both the front and rear. Deques are widely used in applications requiring efficient access and modification at both ends.
Find elsewhere
🌐
PyPI
pypi.org › project › pycopy-collections.deque
pycopy-collections.deque · PyPI
collections.deque module for Pycopy
      » pip install pycopy-collections.deque
    
Published   Jul 14, 2019
Version   0.1.3
🌐
Upgrad
upgrad.com › home › blog › data science › python collections module: counter, chainmap, deque & tuple
Python Collections Module: Counter, ChainMap, Deque & Tuple | upGrad blog
November 24, 2025 - The containers in the Collections module may be quite beneficial for business-level projects and models, adding significantly to the usefulness of generic Python containers through improved optimization and execution speed. Several data types are present in the collection module, such as deque, defaultdict, namedtuple, OrderedDict, Counter, ChainMap, UserDict, UserList, UserString, etc.
🌐
Programiz
programiz.com › java-programming
Learn Java Programming
Java Collection Interface · Java List · Java ArrayList · Java Vector · Java Stack Class · Java Queue · Java Queue Interface · Java PriorityQueue · Java Deque Interface · Java LinkedList · Java ArrayDeque · Java BlockingQueue · Java ArrayBlockingQueue ·
🌐
Udemy
udemy.com › development
The Ultimate Python Bootcamp: Learn by Building 50 Projects
March 6, 2026 - Other collections types like deque, ChainMap, and Counter are briefly noted. The video emphasizes that these advanced types, accessed via import statements, extend Python's core functionalities for specialized tasks, encouraging learners to revisit them as their programming journey progresses ...
Rating: 4.5 ​ - ​ 2.22K votes
🌐
SDSU
edoras.sdsu.edu › doc › Python-Docs-2.5 › lib › deque-objects.html
5.3.1 deque objects
September 19, 2006 - Previous: 5.3 collections Up: 5.3 collections Next: 5.3.1.1 Recipes · Returns a new deque objected initialized left-to-right (using append()) with data from iterable.
🌐
Dummies
dummies.com › article › technology › programming-web-design › python › working-deques-python-250567
Working with Deques in Python | dummies
June 28, 2025 - Unlike some other collections, a deque is fully iterable. This means that you can obtain a list of items by using a for loop whenever necessary. Click Run Cell. Python outputs the information (the screenshot shows only the output and none of the code).
🌐
FavTutor
favtutor.com › blogs › deque-python
Python Deque: Example, Implementation & Methods (with code)
March 15, 2023 - A class called deque can be found in Python's collections module which is optimized for quick operations like appending and popping items from either end of the underlying data structure.
🌐
SSOJet
ssojet.com › data-structures › implement-deque-in-python
Implement Deque in Python | Implement Data Structures in Programming Languages
November 26, 2025 - You can create a deque in Python either empty or pre-populated with elements from an existing iterable, such as a list or tuple. To initialize, simply pass the iterable to the deque constructor. from collections import deque # Initialize with a list my_list = [10, 20, 30] d1 = deque(my_list) print(d1) # Output: deque([10, 20, 30]) # Initialize an empty deque d2 = deque() print(d2) # Output: deque([])
🌐
PyPI
pypi.org › project › pydantic
pydantic · PyPI
Move deque schema gen to GenerateSchema class by @sydney-runkle in #11239 · Move Mapping schema gen to GenerateSchema to complete removal of prepare_annotations_for_known_type workaround by @sydney-runkle in #11247 · Remove Python 3.8 Support by @sydney-runkle in #11258
      » pip install pydantic
    
Published   Apr 20, 2026
Version   2.13.3
🌐
Towards Data Science
towardsdatascience.com › home › latest › python collections module: the forgotten data containers
Python Collections Module: The Forgotten Data Containers | Towards Data Science
March 5, 2025 - The deque should be your go-to data structure when you want a queue and do not care about multithreading. While deques are threadsafe, queues from the Queue module are better suited.
🌐
CodeSignal
codesignal.com › learn › courses › advanced-built-in-data-structures-and-their-usage › lessons › understanding-queues-and-deques-in-python
Understanding Queues and Deques in Python
A deque, or "double-ended queue", allows the addition and removal of items from both ends. Python provides the collections module containing the deque class for implementing deques.
🌐
CodeConverter
codeconverter.com › articles › deque-python
Deque Python — Guide with Examples | CodeConverter Blog
February 11, 2026 - A deque (short for double-ended queue) is a data structure that allows you to efficiently add and remove elements from both ends. It's part of Python's built-in `collections` module and is implemented as a doubly linked list.
🌐
Medium
medium.com › @sanjula99perera › efficient-queue-implementation-in-python-using-collections-deque-2446908ffa34
“Efficient Queue Implementation in Python Using collections.deque” | by Sanjulaperera | Medium
May 28, 2024 - The deque class from the collections module provides an efficient way to implement a queue. It offers O(1) time complexity for both append and pop operations.