Chapter 5

Collections and Iteration

Lists, tuples, dictionaries, sets, indexing, slicing, comprehensions, mutation, and copying.

Subject
Python
Lessons
7 lessons
Practice
12 checks
Review
3 pages

What this chapter does

Real programs rarely work with one value at a time. This chapter teaches the containers Python uses for groups of values, the iteration patterns that read them, and the copying habits that keep state changes visible.

Lessons

Read these in order.

The chapter opening gives the main idea. Move through these lessons next; each page reuses ideas from the pages before it.

  1. 01
    Lists

    Ordered, changeable groups of values and the basic operations they support.

  2. 02
    Tuples and Unpacking

    Fixed small records, multiple return values, and assigning a structure to names.

  3. 03
    Dictionaries

    Keyed lookup, counting, and storing related facts under clear names.

  4. 04
    Sets

    Uniqueness, membership tests, and the small set operations used in data work.

  5. 05
    Indexing and Slicing

    Selecting one item, a range of items, or a simple window from a sequence.

  6. 06
    Comprehensions

    Readable one-pass transformations and filters over existing collections.

  7. 07
    Mutation and Copying

    When two names share one object, when a copy is separate, and why that matters.

  1. Conclusion

    What collections prepare before files, modules, and arrays.

  2. Review

    A compact review of lists, tuples, dictionaries, sets, slices, comprehensions, mutation, and copying.

  3. Exercises

    Chapter-level practice for grouped values, indexing, counting, and mutation.

You are ready when

  • Build and update lists.
  • Unpack tuples and small records.
  • Use dictionaries for keyed counts.
  • Use sets for uniqueness and membership.
  • Use indexing, slicing, and simple comprehensions.
  • Distinguish mutation from copying.

Where this leads

  • Errors and Debugging
  • Files, Paths, and Modules
  • NumPy Arrays

Chapter progress