Skip to contentSkip to navigation

Collections Module Tools

Use the collections module's ready-made tools: Counter for counting, defaultdict for grouping without KeyError, deque for fast queues, and namedtuple for readable records.
  • Module 4, lesson 4
  • About 8 minutes
  • Builds on earlier lessons
  • 12-question quiz at the end

What you will learn

  • Count with Counter
  • Group with defaultdict
  • Queue with deque
  • Name with namedtuple

Ready-made tools in collections

Python's collections module comes with four handy tools. Import one and skip writing it yourself.

Borrowing a tool

One line brings a tool into your program. Tap each part to see its job.

Try each tool

Pick an example, change it, then press Run. Say what you expect to see before you press.

Watch a vote get counted

Press Step to run one line at a time. A class votes for its favourite pet.

Quick guesses

Guess first, then read why. A wrong guess is where the learning happens. Nothing here is scored.

Common collections misconceptions

Each card is something beginners often believe. Tap it to see what Python really does.

Notes

Everything above, in words. Open it when you want the full explanation.

Syntax

The general shape of the statements in this lesson.

Worked examples

Read the problem, run the code, then read why it works.

Common pitfalls

Mistakes people make with this topic, and what to write instead.

In the real world

Where this shows up in software people use every day.

Key takeaways

Common questions

Guess the answer first, then open it.

Your notes

Sign in to keep private notes alongside this lesson.

Sign in to take notes

Where to go next

The quiz and the Lab live on their own pages, so this lesson stays about learning.

Finished this lesson?

Create an ID or sign in to save your progress across devices.

Create a free ID
Lessons done in this module0/4 complete

Quiz

Check what you learned

12 questions, with an explanation after every answer. A finished attempt is marked on the server and earns XP.

Lab

Keep going in the Lab

7 tasks on this lesson, from writing code to tracing it by hand. Practice only, no XP.

Review

Spaced review

Questions you miss in a quiz come back on a spaced schedule, so what you learned here sticks.

This lesson at a glance

The 6 lines of code this lesson uses, each with a short note. Keep it open while you do the quiz and the Lab.

What else you can search for and explore

Curious to go further? Each one opens a web search in a new tab.

References

Look things up here whenever you need the exact details.