Skip to main content
← Python Study Guide

Unit 3 · Heavy

Collections and Algorithms

The four built-in collections and the habit of choosing between them. Most of the awkwardness in beginner Python comes from solving a dictionary problem with parallel lists.

What a strong answer looks like

A strong Unit 3 answer names the collection that matches the problem and says what that choice makes cheap.

Topics in this unit

1

Lists and Slicing

Know

Lists are ordered and mutable. A slice takes start, stop, and step, and the stop index is excluded.

Apply

Mark both slice boundaries before evaluating, and use a negative step to reverse.

Watch out

Treating the stop index as included, which yields a slice one item short of what you expected.

Study move

Predict five slices of one list, including a negative index and a negative step.

2

Dictionaries

Know

A dictionary maps keys to values with constant average lookup. Keys must be hashable, which excludes lists.

Apply

Reach for a dictionary whenever you would otherwise keep two parallel lists in step.

Watch out

Indexing a missing key raises rather than returning nothing; use a default lookup when absence is expected.

Study move

Convert a pair of parallel lists into one dictionary and rewrite a lookup against it.

3

Sets and Tuples

Know

A set stores unique unordered items with fast membership testing. A tuple is an immutable sequence, which makes it hashable and usable as a dictionary key.

Apply

Use a set to deduplicate or test membership, and a tuple for a fixed record.

Watch out

Expecting a set to preserve order. It does not, and relying on the order you observe is fragile.

Study move

Deduplicate a list with a set and explain what is lost.

4

Comprehensions and Iteration Patterns

Know

A comprehension builds a collection from an iterable in one expression, and is clearer than an accumulating loop when the logic is simple.

Apply

Use a comprehension for map-and-filter work; keep a loop when the body needs several statements.

Watch out

Compressing complex logic into one comprehension, which trades readability for brevity.

Study move

Rewrite an accumulating loop as a comprehension, then find one that should stay a loop.

Emphasized in this unit

Connections and techniques that receive extra attention in this unit.

  • Choosing the collection before writing the loop
  • Handling a missing dictionary key deliberately
  • Knowing when a comprehension helps and when it hurts

Varies by course

Related topics some schools attach to this unit and others leave out. Covered on request rather than assumed.

  • collections module. Counter and defaultdict are introduced in some sections.
  • Sorting with keys. Custom sort keys appear here or in the algorithms discussion depending on the course.

Mastery checklist

  • Evaluate a slice with both boundaries marked.
  • Replace parallel lists with a dictionary.
  • Choose between list, dict, set, and tuple for a described problem.
  • Rewrite a simple loop as a comprehension.

Check yourself

  • Why can a list not be a dictionary key?
  • What happens when you index a dictionary with a missing key?
  • When does a comprehension make code worse?

Modeling drill

Given a list of student names with repeated entries, produce a count per name and the list of unique names, choosing structures that make each cheap.

ListSliceDictionaryHashableSetTupleComprehension