Lists

One name keeps one value. But what if there are many: a list of friends, of shopping, of marks? That is what a list is for.

A list of values

A list is written in square brackets, with commas between the values. Items are numbered from 1, so friends[1] is the first one.

friends = ["Olya", "Taras", "Marko"]
say("First:", friends[1])
say("How many:", length(friends))
Open in Studio
Output
First: Olya
How many: 3

length says how many items a list holds. For text it says how many letters there are.

Add to the end

A list can grow while the program runs: append puts a value at its end.

shopping = ["bread"]
append(shopping, "milk")
say(shopping)
Open in Studio
Output
["bread", "milk"]

Walk the list

for … in takes the items one by one and puts each into a name.

shopping = ["bread", "milk", "apples"]
for item in shopping:
    say("Buy:", item)
Open in Studio
Output
Buy: bread
Buy: milk
Buy: apples

Count them all

A list and a loop together give you almost any calculation.

marks = [7, 9, 10, 8]
sum = 0
for mark in marks:
    sum = sum + mark
say("Average:", sum / length(marks))
Open in Studio
Output
Average: 8.5

Try it yourself

  1. Make a list of three favourite dishes and print each one on its own line.
Show the answer
dishes = ["soup", "pancakes", "dumplings"]
for dish in dishes:
    say(dish)
Open in Studio
Output
soup
pancakes
dumplings
  1. Find the largest number in a list.
Show the answer
numbers = [3, 17, 8]
largest = numbers[1]
for n in numbers:
    if n > largest:
        largest = n
say("The largest:", largest)
Open in Studio
Output
The largest: 17