The best way to implement a queue in Python is collections.deque: append() enqueues at the right and popleft() dequeues from the left, both in O(1) time. A plain list works for small cases, but list.pop(0) shifts every remaining element and costs O(n). For threads use queue.Queue, and for asyncio use asyncio.Queue.
A queue is a first-in, first-out (FIFO) structure: the item that has waited longest leaves first. This guide shows the three practical implementations, a reusable class, and how to choose between them.
A list is a dynamic array. Appending to the end is cheap, but removing the first element moves all the others one slot to the left.
For a handful of items this is fine. With hundreds of thousands of items, every dequeue gets slower, and a loop that drains the queue becomes quadratic.
deque (pronounced "deck") is a double-ended queue implemented as a linked list of fixed-size blocks. Adding or removing at either end is O(1).
Calling popleft() on an empty deque raises IndexError, so check if q: first or catch the exception. Indexing in the middle of a deque is O(n), so do not use it for random access.
Passing maxlen creates a bounded buffer. When it is full, adding an item silently discards one from the opposite end, which suits "last N events" buffers but not job queues where nothing may be lost.
Wrapping deque gives you a clear interface and one place to add behaviour. This version is generic, typed and iterable.
On Python 3.12 and newer you can also write the class header with the built-in syntax class Queue[T]: and drop TypeVar.
deque.append() and popleft() are atomic in CPython, but they do not let a consumer wait for new items. When producers and consumers run concurrently, use the standard-library queues, which handle locking and blocking for you.
For coroutines use asyncio.Queue with await q.put(x) and await q.get(). For separate processes use multiprocessing.Queue. If you need items ordered by priority rather than arrival, use queue.PriorityQueue or the heapq module.
| Option | Enqueue | Dequeue | Thread-safe | Best for |
|---|---|---|---|---|
list | O(1) | O(n) | No | Tiny queues, quick scripts |
collections.deque | O(1) | O(1) | Atomic append/pop only | General single-thread FIFO |
queue.Queue | O(1) | O(1) | Yes, blocking | Producer/consumer with threads |
asyncio.Queue | O(1) | O(1) | For one event loop | Coroutine pipelines |
queue.PriorityQueue | O(log n) | O(log n) | Yes | Highest-priority-first work |
Use collections.deque. Both append() and popleft() take constant time, unlike list.pop(0), which is linear.
It copies every remaining element one position, so cost grows with the list length. For a few dozen items you will not notice, but for large queues it dominates run time.
For deque and list, use if not q:. For queue.Queue, q.empty() exists but is only a snapshot in multi-threaded code, so prefer get_nowait() with a queue.Empty handler.
A queue is first in, first out; a stack is last in, first out. With a deque, you get a queue by pairing append() with popleft(), and a stack by pairing append() with pop().
Implement a Python queue with collections.deque, using append() to enqueue and popleft() to dequeue in constant time. Avoid list.pop(0) for anything large, wrap the deque in a small typed class for a clean interface, and switch to queue.Queue or asyncio.Queue when producers and consumers run concurrently.