deque
Use fast double-ended queues.
deque is a free Python Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is a deque?
A deque (pronounced 'deck') is a double-ended queue from the collections module. It supports fast appends and pops from both ends.
Import it with from collections import deque.
from collections import deque
d = deque([1, 2, 3])
print(d)Why not just a list?
Lists are slow when you insert or remove at the front because every element must shift. A deque does front operations in constant time, making it ideal for queues.
from collections import deque
d = deque(['b', 'c'])
d.appendleft('a')
print(d)Appending on both ends
Use .append() to add to the right and .appendleft() to add to the left.
from collections import deque
d = deque()
d.append(1)
d.append(2)
d.appendleft(0)
print(d)Popping from both ends
.pop() removes and returns the rightmost item; .popleft() removes the leftmost. Both are fast.
from collections import deque
d = deque([10, 20, 30])
print(d.pop())
print(d.popleft())
print(d)A simple queue (FIFO)
For first-in-first-out behavior, append on the right and popleft from the left. Items leave in the order they arrived.
from collections import deque
queue = deque()
queue.append('first')
queue.append('second')
print(queue.popleft())
print(queue.popleft())Bounded deque with maxlen
Give a maxlen to cap the size. When full, adding to one end automatically discards an item from the other — great for keeping recent history.
from collections import deque
recent = deque(maxlen=3)
for n in [1, 2, 3, 4, 5]:
recent.append(n)
print(recent)Rotating elements
.rotate(n) shifts items to the right by n steps; a negative n rotates left. Elements wrap around.
from collections import deque
d = deque([1, 2, 3, 4, 5])
d.rotate(2)
print(d)
d.rotate(-1)
print(d)Extending in bulk
.extend() adds many items to the right and .extendleft() adds them to the left. Note that extendleft reverses the order of what you add.
from collections import deque
d = deque([3])
d.extend([4, 5])
d.extendleft([2, 1])
print(d)Indexing and length
A deque supports indexing like a list and works with len(). Accessing the middle is slower than the ends, so prefer end operations.
from collections import deque
d = deque(['a', 'b', 'c'])
print(d[0])
print(d[-1])
print(len(d))Clearing and reversing
.reverse() flips the deque in place, and .clear() empties it. Both modify the deque directly.
from collections import deque
d = deque([1, 2, 3])
d.reverse()
print(d)
d.clear()
print(d)Counting occurrences
Like a list, a deque has .count() to tally how often a value appears.
from collections import deque
d = deque([1, 2, 2, 3, 2])
print(d.count(2))Quick Check
You build a FIFO queue with a deque. Which pair of methods gives correct first-in-first-out order?
Recap: deque
You learned that a deque:
- Supports fast
append/popon both ends, unlike a list. - Has
appendleftandpopleftfor the left side. - Can be bounded with
maxlento keep only recent items. - Offers
rotate,extend,reverse, andcountfor queue-style work.
from collections import deque
q = deque(maxlen=2)
for x in ['a', 'b', 'c']:
q.append(x)
print(q)Frequently asked questions
Is the “deque” lesson free?
Yes — the full text of “deque” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “deque”?
Use fast double-ended queues. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “deque” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.