Chapter 6

Dictionaries, Sets, and Copies

Organize readings by meaningful keys, distinguish counts from groups, track unique labels, and predict whether a change reaches shared or copied data.

An ordered list answers questions about position. A measurement program also asks questions such as “What is the east reading?”, “How many readings were high?”, and “Which readings came from north?” These questions need meaningful keys, repeated updates, and sometimes a complete group of values under one label.

Dictionaries will provide the keyed lookup, counts, and groups. Sets will handle a narrower question: whether a label occurs and how two groups of labels differ. Each collection will be chosen for the question it answers rather than because its syntax is available.

The final lessons examine what assignment and copying mean for mutable collections. We will draw names, outer collections, and nested values separately, so a change is never explained as if it moved mysteriously between variables.

After this chapter

  • Retrieve, add, and replace dictionary values by meaningful string keys while handling absence according to the data contract.
  • Build keyed counts from repeated labels and report keys, values, or item pairs in a stated order.
  • Group several readings under each sensor and distinguish a complete group from a count or latest value.
  • Use sets for membership, uniqueness, intersection, union, and directional difference without relying on display order.
  • Predict mutation through aliases and function parameters while distinguishing reassignment, equality, and identity.
  • Create a shallow outer copy deliberately and identify nested mutable values that remain shared.

Lessons

  1. 01
    Look Up Values by Key

    Create dictionaries, retrieve and update values by meaningful keys, handle absence according to the data contract, and sort stated reports.

    3 exercises
  2. 02
    Count by Key

    Build keyed frequency counts, inspect keys, values, and items, unpack pairs, and report counts predictably.

    3 exercises
  3. 03
    Group Values by Key

    Group repeated values into list-valued dictionaries, trace outer and inner state, and derive one result from each complete group.

    3 exercises
  4. 04
    Track Membership and Uniqueness with Sets

    Track unique labels, test membership, add values, and compare runs with intersection, union, and directional difference.

    3 exercises
  5. 05
    See When Two Names Share One Collection

    Trace aliases and mutation, distinguish reassignment, follow mutable arguments, and separate value equality from object identity.

    3 exercises
  6. 06
    Copy Without Hiding Shared Data

    Make shallow copies of lists, dictionaries, and sets; predict nested sharing; and return changed copies without mutating caller-owned data.

    3 exercises

Review and practice

  1. Review

    Review dictionary lookup, counting, grouping, sets, aliases, reassignment, identity, and shallow copying.

  2. Exercises

    Practice keyed lookup, frequency counts, grouping, dictionary items, set comparison, aliases, reassignment, and shallow copying.

Chapter progress