🌐
GeeksforGeeks
geeksforgeeks.org › python › queue-in-python
Queue in Python - GeeksforGeeks
May 29, 2026 - There are various ways to implement a queue in Python by following ways: Lists can be used as queues, but removing elements from front requires shifting all other elements, making it O(n). ... q = [] q.append('a') q.append('b') q.append('c') print("Initial queue:", q) print("Elements dequeued from queue:") print(q.pop(0)) print(q.pop(0)) print(q.pop(0)) print("Queue after removing elements:", q)
🌐
CodeSignal
codesignal.com › learn › courses › linked-lists-stacks-and-queues-in-python › lessons › understanding-and-implementing-queues-exploring-core-concepts-python-implementation-and-time-complexity
Understanding and Implementing Queues: Exploring Core ...
For example, consider a line of people waiting to buy tickets at a theater. The person who arrives first gets their ticket first. In computer science, a Queue works in exactly the same way. In Python, we can implement Queues using built-in data types. Indeed, the Python list datatype comes ...
Discussions

Is popleft() faster than pop(0) ?

Yes. list.pop(0) is O(n), and deque.popleft() is O(1).

More on reddit.com
🌐 r/learnpython
9
6
May 14, 2020
How is python BFS O(E + V). Doesn’t dequeuing using queue.pop(0) require O(n?
If you look at any python implementation of bfs, it always uses a "deque" (double ended queue) instead of a "list". The only difference being a deque is O(1) for popping the first element, if you used a normal list then that would be O(n). More on reddit.com
🌐 r/leetcode
14
16
March 12, 2022
I was surprised at how slow list.pop() is! And list.remove() is even many times slower
This is simply how lists work, nothing surprising here. Removing the first element requires moving all the elements after it one step to the left to fill that gap, which makes this operation run in linear time. It means that clearing the list this way is O(n2), so it unsurprisingly takes a long time, as bubble sorting the (shuffled) list could even be faster. This is why we think of alternatives when solving problems, such as using collections.deque, reversing the list (O(n) instead of O(n2)) or just using pop() from the end if it works. More on reddit.com
🌐 r/learnpython
38
57
August 22, 2021
🌐
Reddit
reddit.com › r/learnpython › best way to continuously pop elements from a queue until condition is reached
r/learnpython on Reddit: Best way to continuously pop elements from a queue until condition is reached
April 23, 2024 -

I have a deque that consists of datetime objects (the rightmost elements will always be newer) and I'm trying to write some logic to pop items older than n seconds off the queue. I have it sort of functioning, but my while loop depends on looking at the -1 element of the queue, which doesn't always exist (in the case where it has been emptied because all items in the queue were older than the threshold). I can get around it with some try/except stuff or more conditionals, but none of that seems very pythonic.

while True:
    targettime = datetime.now() - timedelta(seconds=5 * 60)
    while queue[-1] >= targettime and len(queue) > 0:
        queue.pop()
    if len(queue) == 0:
        do_work()
    time.sleep(60)

Any advice on how to handle this better? I'm open to entirely new solutions as well, using a deque seemed like the best approach but now I'm not so sure.

something's wrong with pop() function Jun 17, 2023
r/learnpython
3y ago
Could somebody help me understand the Queue? Feb 16, 2018
r/learnpython
8y ago
Just a quick question about .pop() in python Jan 24, 2020
r/learnprogramming
6y ago
I need help with asyncIO queues Apr 17, 2020
r/learnpython
6y ago
More results from reddit.com
🌐
Medium
medium.com › @mollihua › pop-first-element-of-a-queue-in-python-list-pop-0-vs-collections-deque-popleft-7991408e45b
Pop first element of a queue in Python — list.pop(0) vs deque.popleft() | by mollihua | Medium
July 2, 2020 - def listpop(alist): ts = time.time() alist.pop(0) te = time.time() print("{:e}".format(te - ts))def dequepopleft(alist): q = collections.deque(alist) ts = time.time() q.popleft() te = time.time() print("{:e} seconds".format(te - ts))a = [x for x in range(10**6)]listpop(a) # output: 4.558802e-03 seconds dequepopleft(a) # output: 2.861023e-06 seconds · Python · Queue · 0 followers ·
🌐
W3Schools
w3schools.com › python › python_dsa_queues.asp
Queues with Python
Using a Python list as a queue: queue = [] # Enqueue queue.append('A') queue.append('B') queue.append('C') print("Queue: ", queue) # Peek frontElement = queue[0] print("Peek: ", frontElement) # Dequeue poppedElement = queue.pop(0) print("Dequeue: ", poppedElement) print("Queue after Dequeue: ", queue) # isEmpty isEmpty = not bool(queue) print("isEmpty: ", isEmpty) # Size print("Size: ", len(queue)) Try it Yourself » ·
🌐
University of Vermont
uvm.edu › ~cbcafier › cs1210 › book › 11_loops › stacks_and_queues.html
Stacks and queues – Clayton Cafiero
June 24, 2025 - With a queue, we enqueue from one end and dequeue from the other. Like a stack, we can use append to enqueue. The little twist is that instead of .pop() which would pop from the same end, we use .pop(0) to pop from the other end of the list, and voilà, we have a queue.
🌐
Llego
llego.dev › home › blog › implementing a queue in python: a step-by-step guide
Implementing a Queue in Python: A Step-by-Step Guide - llego.dev
August 5, 2023 - We will create a Queue class containing ... enqueue(self, item): self.items.append(item) def dequeue(self): if self.isEmpty(): return "Queue is empty" return self.items.pop(0) def peek(self): if self.isEmpty(): return "Queue is empty" return ...
🌐
iO Flood
ioflood.com › blog › python-queue
Python Queue Class | Usage Guide (With Examples)
February 5, 2024 - # Create a queue using a list list_queue = [] # Add items to the queue list_queue.append('Apple') list_queue.append('Banana') list_queue.append('Cherry') # Remove items from the queue print(list_queue.pop(0)) # Output: 'Apple' print(list_queue.pop(0)) # Output: 'Banana' Python’s collections.deque is a double-ended queue.
🌐
CodingNomads
codingnomads.com › python-301-use-a-list-in-stack-or-queue
Use a List in a Stack or Queue
.pop(0), used with an argument of 0, allows you to remove the first element from the collection. Just like in the stack example above, you can also check whether your queue is empty and peek at the first item of the list without removing it.
Find elsewhere
🌐
Medium
basillica.medium.com › working-with-queues-in-python-a-complete-guide-aa112d310542
Working with Queues in Python — A Complete Guide | by Basillica | Medium
March 27, 2024 - Here is one way to implement a ... enqueue(self, item): self.items.append(item) def dequeue(self): if self.size() == 0: return None return self.items.pop(0) To use it: q = Queue() q.enqueue(1) q.enqueue(2) q.enqueue(3) ...
🌐
Python Morsels
pythonmorsels.com › stacks-and-queues
Stacks and queues in Python - Python Morsels
June 10, 2026 - So if you need a stack-like structure in Python, use a list. ... Note that these items are added to the right-hand side (the end) of our deque. If we then use the popleft method to repeatedly remove items, the first item that was added will be the first item that was removed: >>> while queue: ... print(queue.popleft()) 0 1 2 3 4
🌐
Readthedocs
pynote.readthedocs.io › en › latest › DataTypes › Stack_Queue.html
Stacks and Queues in Python — pynotes documentation
fruits = [] fruits.append('banana') fruits.append('grapes') fruits.append('mango') fruits.append('orange') first_item = fruits.pop(0) print(first_item) first_item = fruits.pop(0) print(first_item) print(fruits) ... Again, here we use the append and pop operations of the list to simulate the ...
🌐
Board Infinity
boardinfinity.com › blog › queue-in-python
Queue in Python | Board Infinity
August 9, 2025 - By using Deque, we can do push and pop operations in O(1) time complexity from both the ends of the collection. So, in some cases Deque is preferred over the list as they are faster as compared to the list. ... Python's built-in module Queue is used to implement queues. queue. Queue(maxsize) sets a variable's initial value to maxsize, its maximum size. An infinite queue is indicated by a maxsize of zero "0...
🌐
Analytics Vidhya
analyticsvidhya.com › home › queue in python: an in-depth guide
Queue in Python: An In-Depth Guide
August 12, 2024 - Python lists can be used to implement a queue. However, using lists for queues is not efficient for large datasets because removing elements from the front of a list is an O(n) operation. class ListQueue: def __init__(self): self.queue = [] def enqueue(self, item): self.queue.append(item) print(f"Enqueued: {item}") def dequeue(self): if self.is_empty(): raise IndexError("Dequeue from an empty queue") item = self.queue.pop(0) print(f"Dequeued: {item}") return item def peek(self): if self.is_empty(): raise IndexError("Peek from an empty queue") print(f"Peek: {self.queue[0]}") return self.queue[0] def is_empty(self): return len(self.queue) == 0 def size(self): print(f"Size: {len(self.queue)}") return len(self.queue) def clear(self): self.queue = [] print("Queue cleared") # Example usage lq = ListQueue() lq.enqueue(1) lq.enqueue(2) lq.peek() lq.dequeue() lq.size() lq.clear()
🌐
Python
docs.python.org › 3 › library › queue.html
queue — A synchronized queue class
The count of unfinished tasks goes up whenever an item is added to the queue. The count goes down whenever a consumer thread calls task_done() to indicate that the item was retrieved and all work on it is complete. When the count of unfinished tasks drops to zero, join() unblocks. Example of how to wait for enqueued tasks to be completed:
🌐
Boot.dev
boot.dev › blog › python › queue data structure in python: ordering at its best
Queue Data Structure in Python: Ordering at Its Best | Boot.dev
June 18, 2026 - Here's a simple list-based queue: class Queue: def __init__(self): self.items = [] def push(self, item): self.items.insert(0, item) def pop(self): if len(self.items) == 0: return None return self.items.pop() def peek(self): if len(self.items) ...
🌐
Medium
medium.com › @shuangzizuobh2 › how-well-do-you-code-python-9bec36bbc322
How slow is python list.pop(0) ?. An empirical study on python list.pop… | by Hj | Medium
September 27, 2023 - To implement a queue, use collection.deque which was designed to have fast appends and pops from both ends. Essentially, a Python list is implemented as an array of pointers. The list.pop(k) first removes and returns the element at k, and then moves all the elements beyond k one position up. Here is a demo example of using Queue to level-order traverse (generate) a tree.
🌐
Runestone Academy
runestone.academy › ns › books › published › pythonds › BasicDS › ImplementingaQueueinPython.html
4.12. Implementing a Queue in Python — Problem Solving with Algorithms and Data Structures
class Queue: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def enqueue(self, item): self.items.insert(0,item) def dequeue(self): return self.items.pop() def size(self): return len(self.items) CodeLens 1 shows the Queue class in action as we perform the sequence of operations from Table 1. Activity: CodeLens Example Queue Operations (ququeuetest)
🌐
Software Testing Help
softwaretestinghelp.com › home › python programming for beginners – free python tutorials › python queue tutorial: how to implement and use python queue
Python Queue Tutorial: How To Implement And Use Python Queue
April 1, 2025 - This Python Queue tutorial will discuss pros, cons, uses, types, and operations on Queues along with its implementation with programming examples: In Python, a Queue is a linear data structure that follows the FIFO approach. Here FIFO refers to “ First In First Out “ i.e. the first element entered in the queue will be popped out first.
🌐
Intellipaat
intellipaat.com › home › blog › python queue tutorial: queue module, deque & priority queue (2026)
Python Queue: queue.Queue, deque & PriorityQueue Explained with Examples
May 7, 2026 - In this script, the enqueue() function adds an element to the queue by appending it to the end of the queue list. The dequeue() function removes the first element from the queue using the pop(0) method.