Lists — Pseudocode

A list keeps many values in one variable: sales ← [15, 8, 22]. You can also start from an empty list [], then add items at the end with APPEND(sales, price)…

At nine at night, Mrs. Sri sends her nephew a WhatsApp message:

Today's sales: $15, $8, $22, $5, $12. What's the total? And what were the first and the last?

There are five numbers in one message. Until this lesson, one variable has held only one value, so five numbers need five variables: sale1, sale2, up to sale5. If there are eight sales tomorrow, the program has to be written again.

Mrs. Sri's message already shows the way: write all the numbers in a row, in one place. In pseudocode, a row of values kept in one variable is called a list.

One name, many items

This program keeps those five sales in one list named sales. The result is below the code:

sales ← [15, 8, 22, 5, 12]
PRINT sales
PRINT LENGTH(sales)
[15, 8, 22, 5, 12]
5

A list is written between square brackets [ ], with commas between its items (the values in it). LENGTH(sales) counts how many items the list has, which is 5. A list can hold text too, like menu ← ["Iced tea", "Coffee", "Fried rice"].

Item numbers start at 0

Every item in a list has a number. To take one item, write the name of the list, then the number of the item in square brackets. Before you read on, guess: is sales[1] 15 or 8?

sales ← [15, 8, 22, 5, 12]
PRINT sales[0]
PRINT sales[1]
PRINT sales[4]
15
8
12

sales[1] is 8, the second item. The first item is number 0. Five items are numbered 0, 1, 2, 3, 4. So the number of the last item is 4, one less than the length of the list. This item number is usually called the index.

Some school books count from 1, so in those books the first item is sales[1]. In this lesson, and later in JavaScript and Python, counting starts at 0. If you read a book that starts at 1, subtract one from each of its numbers.

Because the numbers start at 0, the last item is always at number LENGTH(sales) - 1, however long the list is:

sales ← [15, 8, 22, 5, 12]
last ← LENGTH(sales) - 1
PRINT "Item", last, "holds", sales[last]
Item 4 holds 12

You can also replace an item through its number. For example, Mrs. Sri wrote one sale down wrong: the second sale was 9, not 8. The second sale is at number 1:

sales ← [15, 8, 22]
sales[1] ← 9
PRINT sales
[15, 9, 22]

APPEND: a new item at the end

A list does not have to be typed in full from the start. You can begin with an empty list, then add items one by one:

sales ← []
APPEND(sales, 15)
APPEND(sales, 8)
PRINT sales
PRINT LENGTH(sales)
[15, 8]
2

[] is an empty list. APPEND(sales, 15) puts 15 at the end of the list sales, and the list gets one item longer. APPEND goes on a line of its own, without ←, because APPEND changes that list directly and does not give back a new list.

When you use APPEND together with WHILE, a list can be filled from the input until a 0 is read, however many values come. With the input 15, 8, 22, 0:

sales ← []
READ price
WHILE price > 0 DO
    APPEND(sales, price)
    READ price
END WHILE
PRINT sales
[15, 8, 22]

FOR EACH: one round per item

To add up all the items of a list, use a loop that runs once for each item:

sales ← [15, 8, 22, 5, 12]
total ← 0
FOR EACH price IN sales
    total ← total + price
END FOR
PRINT "Total:", total
Total: 62

FOR EACH price IN sales starts at item number 0 and ends at the last item. In each round, the variable price holds the item whose turn it is: 15 in the first round, 8 in the second, and so on. You do not need to write a number or LENGTH.

If you need the item number as well, use a plain FOR, from 0 to LENGTH - 1:

menu ← ["Iced tea", "Coffee", "Fried rice"]
FOR i ← 0 TO LENGTH(menu) - 1
    PRINT i, menu[i]
END FOR
0 Iced tea
1 Coffee
2 Fried rice

Look at the - 1 on the FOR line. Without the - 1, the last round asks for menu[3], but the item numbers of menu only go up to 2.

In JavaScript later, a list is called an array. In Python it is called a list. Both use the same square brackets, and their item numbers start at 0 too.

⚠️ A number past the end

TL;DR: A list keeps many values in one variable: sales ← [15, 8, 22]. You can also start from an empty list [], then add items at the end with APPEND(sales, price). You take an item by its number, and the numbers start at 0. LENGTH(sales) counts the items. So sales[0] is the first item and sales[LENGTH(sales) - 1] is the last. Some books count from 1, but JavaScript and Python start at 0 too. FOR EACH price IN sales is a loop that runs once for each item, with price holding that item. A number past the end of the list stops the program, with a message that says how many items there really are.

⚡ One name, many items

Write:

sales ← [15, 8, 22, 5, 12]
PRINT sales
PRINT LENGTH(sales)

Meaning: A list keeps many values in one variable, here five sales in sales. A list is written between square brackets [ ], with commas between its items. LENGTH(sales) counts how many items it has: 5.

⚡ Item numbers start at 0

Write:

sales ← [15, 8, 22, 5, 12]
PRINT sales[0]
PRINT sales[1]
PRINT sales[4]
Guess first: is sales[1] 15 or 8?

8, the second item. The first item's number is 0.

Meaning: sales[1] takes the item with number 1 from the list sales. Five items are numbered 0 to 4, so the last item is always at number LENGTH(sales) - 1. Some books count from 1, but in this lesson, in JavaScript and in Python the numbers start at 0.

⚡ APPEND: a new item at the end

Write:

sales ← []
APPEND(sales, 15)
APPEND(sales, 8)
PRINT sales
PRINT LENGTH(sales)

Meaning: [] is an empty list. APPEND(sales, 15) puts 15 at the end of the list sales, and the list gets one item longer. APPEND goes on a line of its own, without ←, because APPEND changes that list directly.

⚡ One round per item

Write:

sales ← [15, 8, 22, 5, 12]
total ← 0
FOR EACH price IN sales
    total ← total + price
END FOR
PRINT "Total:", total
Guess first: the loop FOR i ← 0 TO LENGTH(menu) - 1 takes menu[i] in each round. If the - 1 is removed, what happens in the last round?

The program stops with an error. In the last round, i equals the length of the list, but the last item number is one less than the length.

Meaning: FOR EACH price IN sales runs once for each item of the list sales. In each round, price holds the item whose turn it is, so you do not need to write a number or LENGTH. The code above adds up all five items: Total: 62.

What you will learn