Concepts / Working with Lists and Append

Working with Lists and Append

The sentinel value ends collection. It is a control signal, not one of the numbers in the list.

  • Programming

From Input to a Complete List

A program that collects numbers from a user has two separate jobs. First, it builds a list by accepting numbers one at a time. Second, after the user signals that there are no more numbers, it examines the completed list to find its largest and smallest values. Keeping these jobs separate makes the program easier to trace and helps ensure that max() and min() are used at the correct time.

Key point

The sentinel value ends collection. It is a control signal, not one of the numbers in the list.

Watching append Build the List

Imagine the user enters 6, then 2, then 9, then 3, then 5, and finally done. Each number is converted to a numeric type and appended to the list. The list therefore grows after every numeric entry. When done is entered, collection stops, and done is not appended.

Empty list[]6[6]2[6, 2]9[6, 2, 9]3[6, 2, 9, 3]5[6, 2, 9, 3, 5]donestop; list unchangedappend 6append 2append 9append 3append 5sentinel entered
How does each user-entered number move into the list, and what happens next when the sentinel value is entered?
User inputActionList after the iteration
6Convert to a numeric type and append[6]
2Convert to a numeric type and append[6, 2]
9Convert to a numeric type and append[6, 2, 9]
3Convert to a numeric type and append[6, 2, 9, 3]
5Convert to a numeric type and append[6, 2, 9, 3, 5]
doneStop collecting; do not append[6, 2, 9, 3, 5]

The list changes only after numeric input. The sentinel leaves the completed list unchanged.

Separating Collection from Analysis

The input loop is responsible for collecting data. During the loop, the program converts each number to a numeric type and appends it to the list. It does not yet call max() or min(). Those functions are called after the loop exits, when the list is complete. max() finds the largest value in the completed list, while min() finds the smallest value.

User inputnumbers or doneCollect numbersconvert and appendCompleted list[6, 2, 9, 3, 5]max()largest value: 9min()smallest value: 2numeric inputafter doneanalyze after loopanalyze after loop
When are max() and min() called, and how do they use the completed list?
Worked example

Finding the two extremes

The collected numbers are 6, 2, 9, 3, and 5. Determine the results after the user enters done.

  1. 1
    Complete collection

    After the sentinel is entered, the list contains [6, 2, 9, 3, 5].

  2. 2
    Find the largest

    Call max() on the completed list. The largest value is 9.

  3. 3
    Find the smallest

    Call min() on the completed list. The smallest value is 2.

Result

The maximum is 9 and the minimum is 2.

Tracing the Sentinel Branch

Each input has one of two roles. A numeric input belongs in the list, so it is converted to a numeric type and appended. The sentinel belongs to the control flow, so it ends the loop and remains outside the list. This distinction is the key to tracing the program correctly.

Read inputInput is doneStop loopkeep list unchangedConvert numbernumeric typeAppend valueadd to listinputyesnonumeric valuenext input
How does the program branch when the user enters a number compared with when the user enters done?
Before

List before the sentinel: [6, 2, 9, 3, 5]

The list is complete after the last numeric input.

After

List after the sentinel: [6, 2, 9, 3, 5]

Entering done stops collection; no sentinel is added.

Mistakes That Distort the Result

  • Common mistake

    Calling max() or min() inside the loop

    What it looks like

    The program tries to calculate the largest and smallest values while the list is still being built.

    Why it fails

    The intended calculation uses the completed list after the loop exits. During collection, the data is not yet complete.

    Do this instead

    Build the list first, stop when the sentinel is entered, and call max() and min() afterward.

  • Common mistake

    Forgetting to convert input to a number

    What it looks like

    A user-entered number is treated as input text instead of being converted to an int or float.

    Why it fails

    The collection task requires numeric values in the list.

    Do this instead

    Convert each non-sentinel input to a numeric type before appending it.

  • Common mistake

    Appending the sentinel

    What it looks like

    The value done is placed in the same list as the numbers.

    Why it fails

    The sentinel is a stopping signal, not a number to analyze.

    Do this instead

    Check for the sentinel before conversion and append; stop without adding it.

Edge case

If the user enters the sentinel before entering any numbers, the list remains empty. This is an important case to check while debugging because max() and min() are intended to operate after numbers have been collected. The source guidance emphasizes completing the numeric collection before applying these functions.

Practice the State Changes

EASY

Trace this input sequence: 8, 1, 4, done. Write the list after each input. Then identify the values that max() and min() should find after the loop exits.

Hints
  • Start with an empty list.
  • Append each number, but do not append done.
  • Call max() and min() only after the final list is complete.

What do you think happens?

After the inputs 8, 1, 4, done, what is the completed list, and what do max() and min() find?

  • [8, 1, 4, done]; largest 8; smallest 1

  • [8, 1, 4]; largest 8; smallest 1

  • [8, 1, 4]; largest 4; smallest 8

Reveal answer
Answer:

[8, 1, 4]; max() finds 8 and min() finds 1.

The three numeric inputs are appended. The sentinel ends collection without entering the list. The largest and smallest values are then found from the completed list.

What to Remember

  1. 1
    Use a while loop to continue collecting input until a sentinel such as done is entered.
  2. 2
    Convert each numeric input to a numeric type before appending it to the list.
  3. 3
    Never append the sentinel value itself.
  4. 4
    Call max() and min() after the loop exits, when the list is complete.
  5. 5
    When debugging, check the timing of max() and min(), numeric conversion, sentinel handling, and the possibility of an empty list.

At a glance

  • A sentinel-controlled loop separates numeric data from the signal that ends collection.
  • append() grows the list one numeric value at a time.
  • The sentinel is not part of the list.
  • max() and min() should be called after collection finishes.
  • Tracing the list after every input exposes errors in conversion, appending, and loop control.

Related concepts

More concepts starting with W