Unit 3: Programming · Lists

Article

Lists as data abstraction

7 min read

You are writing a program for a class of 3 students. Next week the class has 30. If your program has a separate variable for each student, you must rewrite it. If it has a list, you change almost nothing. This is why lists are such a big idea in programming.

One name for many values

Suppose a program must find the average of three scores. Here is one way, with a variable for each score:

AP pseudocode
score1 ← 72
score2 ← 84
score3 ← 90
total ← score1 + score2 + score3
DISPLAY(total / 3)

Now the same job with a list:

AP pseudocode
scores ← [72, 84, 90]
total ← 0
FOR EACH s IN scores
{
    total ← total + s
}
DISPLAY(total / LENGTH(scores))

You have not met FOR EACH yet (the next article teaches it). For now, read it as "do this once for every element in the list". Both programs display 82. Python does the same:

Runs in your browser

Python shows 82.0, because / gives a decimal number. Now add a fourth score to the list, for example , 100 at the end, and run it again. You only changed the data. The code stayed the same and it still gives the right average. In the variable version, you would add a new variable, change the total line and change the 3 to a 4: three changes, and any one you forget gives a wrong answer.

Check your understanding

A teacher stores 30 test scores in one list instead of in 30 separate variables. Which statement is a benefit of this?

What is a data abstraction?

An abstraction hides the details so you can think about the big picture. A data abstraction gives a collection of data one name, so the rest of the program can use it without caring how many values there are or how each one is stored. The list scores is a data abstraction: the program says "the scores" and does not mention score 1, score 2 or score 3.

Using a list this way helps in four ways:

BenefitExample
Less code and fewer namesone name scores instead of 30 variables
The same code works for any sizethe average code above works for 3 or 3,000 scores
Easier to changeadding or removing a score changes the list, not the code
Easier to reada good name (scores, playerNames) shows what the data is for

A list also makes a procedure simpler. A procedure that finds an average of 30 numbers would need 30 parameters. With a list it needs one:

AP pseudocode
PROCEDURE average(numbers)
{
    total ← 0
    FOR EACH n IN numbers
    {
        total ← total + n
    }
    RETURN(total / LENGTH(numbers))
}

You can call average(scores) with a list of any length.

Check your understanding

A procedure must work out the average of a set of scores. It must work for 5 scores or for 500. Which design is the simplest?

Worked example: explain it in words

The exam (and the Create task) may ask: "Explain how the list manages complexity in your program." A strong answer has two parts:

  1. What the list does: "The list playerNames stores all the players' names under one name."
  2. What would be harder without it: "Without the list I would need a separate variable for every player, and I would have to write new code each time a player joined or left."

A weak answer says "it makes the program faster" or "it makes the code shorter" and stops there. A list does not make a program run faster, and shorter code alone is not the reason. The reason is that the program becomes easier to write, read and change, because the data is managed as one thing.

Common mistakes

  • "A list makes the program run faster." Not the point. It makes it simpler to build and to change.
  • "A list saves memory." The values still take space. The abstraction is about the programmer's job.
  • "Data abstraction means hiding the list from the user." It means the program uses a name for the data without caring about the details.
  • Forgetting the "without it" half of an explanation. Say what the program would need to do instead.
  • Thinking a list is always the best choice. For two unrelated values (a name and an age), two variables are clearer.

On the exam

Expect questions such as: "Which of the following is the best reason to store the scores in a list?" The right answer talks about one name for many values, code that works for any number of items or being easier to change. Wrong answers talk about speed or memory. These ideas also appear in the written responses of the Create task.

Key words

Data abstraction: giving a collection of data a name so the program can use it without dealing with each separate value.

Manage complexity: make a program easier to understand, write and change.

Maintain: keep a program working and update it later.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.