Chapter 5

Lists and Ordered Values

Select, build, pair, summarize, transform, and order familiar values while keeping the ordinary loop behind each compact Python form visible.

Chapter 4 treated `readings` as one input. Here we will inspect one position or range, collect a new result list, and transform one short text label without losing sight of the source values.

Positions become more useful when they remain aligned with labels and readings. Small tuples, `enumerate`, and `zip` will make those relationships visible. Familiar accumulator loops will then lead to `sum`, `min`, `max`, `any`, and `all` only after their meaning and empty-input boundaries are clear.

The final lessons derive a list comprehension from its ordinary loop, use the same form directly inside a known summary, and order labelled results through a named comparison key. Every compact form can be expanded into work that the reader already knows how to trace.

After this chapter

  • Select one item or an ordinary range from a list, tuple, or string and guard an empty sequence before indexing.
  • Build a result list with append while distinguishing mutation from the method's None return value.
  • Trace strip, lower, split, and join through one short text transformation without changing the source string.
  • Unpack fixed pairs and align positions, labels, and readings with enumerate and checked zip calls.
  • Relate sum, min, max, any, and all to familiar loops and state their relevant empty-input behavior.
  • Expand list comprehensions and generator expressions into visible loops, choosing a named result list when inspection matters.
  • Order labelled results with sorted and a named key while preserving the source sequence.

Lessons

  1. 01
    Select from Lists and Text

    Select items and ranges from lists, tuples, and text; test membership; and guard empty sequences.

    3 exercises
  2. 02
    Build and Change Lists

    Build result lists with append, trace visible mutation, and keep method return values separate from changed lists.

    3 exercises
  3. 03
    Split and Join Text

    Transform a short text value with strip, lower, split, and join without changing its source string.

    3 exercises
  4. 04
    Pair and Unpack Values

    Unpack fixed pairs, pair values with positions, zip aligned sequences, and reject unequal lengths when truncation would be wrong.

    3 exercises
  5. 05
    Summarize Existing Sequences

    Summarize existing numerical and Boolean sequences with checked standard operations and explicit empty boundaries.

    3 exercises
  6. 06
    Transform Values with a Comprehension

    Build transformed lists with readable comprehensions and expand compact forms back into ordinary loops.

    3 exercises
  7. 07
    Summarize Transformed Values

    Read generator expressions inside known summaries and expand them into visible lists or loops when inspection matters.

    3 exercises
  8. 08
    Order Values Predictably

    Use sorted, reverse order, named key functions, tuple tie-breaking, and one equivalent simple lambda.

    3 exercises

Review and practice

  1. Review

    Review selection, mutation, text parts, paired values, summaries, comprehensions, generators, and sorting.

  2. Exercises

    Practice selection, list construction, text transformation, pairing, summaries, comprehensions, generators, and named sorting keys.

Chapter progress