Editor

Build a Recent History Tracker

Intermediate · Write the code · from Python collections: Counter, defaultdict, deque, and More

From the lesson: Python collections: Counter, defaultdict, deque, and More

Imagine you have a basic toolbox with a hammer and a screwdriver. They work fine for simple jobs. But when you're building a deck, you really want a power drill, a level, and a tape measure.

Read the lesson

Create a deque with maxlen=3 called history. Append the values 1 through 5 one at a time. After the loop, print the deque converted to a list. It should only contain the last 3 items.

from collections import deque
# Create a deque that keeps only the last 3 items
# Append values 1 through 5
# Print as a list

More collections exercises

All Python exercises · 50 practice problems · Standard Library Essentials quiz