Lists: one-dimensional arrays
Making, indexing and changing a list; append, remove, in, split and join.
Do this lesson in the simulatorA variable holds one value. A list holds many, in order, under one name. Exams call this an array. The robot's depth sensor gives 64 readings at once, as a list, and by the end of this module you will store whole surveys of a room in lists.
Making and reading a list
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
legs = [20, 15, 30]
print(legs)
print("first:", legs[0])
print("last:", legs[2])
print("also last:", legs[-1])
print("how many:", len(legs))
Square brackets make a list, with commas between the items (also called elements). Items are numbered from 0, exactly like the characters of a string, so legs[0] is the first and legs[2] the third. len() counts them. Asking for legs[3] would be an IndexError: there is no fourth item.
Using items
An item is used like any variable:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
legs = [20, 15, 30]
forward(60, distance=legs[0])
right(60, distance=legs[1])
forward(60, distance=legs[2])
print("total:", legs[0] + legs[1] + legs[2])
Changing a list
Unlike a string, a list can be changed after it is made:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
notes = [262, 294, 330]
notes[0] = 523 # replace the first item
notes.append(349) # add one to the end
print(notes, "has", len(notes), "notes")
notes.remove(294) # take out the first 294
print(notes)
last = notes.pop() # take the last item off, and keep it
print("popped", last, "leaving", notes)
list[index] = value replaces an item. append adds to the end, so the list grows. remove takes out the first item equal to a value; pop takes the last item off and gives it back.
An empty list that fills up
Many lists start empty and grow as a program runs:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
readings = []
for i in range(4):
readings.append(distance())
forward(50, distance=10)
print("readings:", readings)
print("the second reading was", readings[1])
Collecting readings into a list and looking at them afterwards is how you find out what a sensor actually does.
Is it in the list?
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
colours = ["red", "green", "blue"]
answer = input("Pick a colour: ")
if answer in colours:
led(answer)
print("item", colours.index(answer), "of the list")
else:
print("not one of", colours)
in works on lists the way it works on strings. index gives the position of an item.
Lists and strings
A string is a sequence of characters and a list is a sequence of anything, so many things work on both: indexing, slicing, len, in. split and join convert between them:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
line = "30,20,30,20"
sides = line.split(",")
print(sides)
print(int(sides[0]) + int(sides[1]))
print(" then ".join(sides))
split breaks a string into a list of strings at every comma. The items are still text, so int is needed before adding them.
Task: a changing tune
Start with notes = [262, 294, 330]. Change the first note to 523, add 349 to the end, print how many notes there are as 4 notes, then play every note in the list for 0.3 seconds. Change the list with list operations, not by typing a new list.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
notes = [262, 294, 330]
Challenges
- Make a list of four colours and show each on the LED with a loop over the indexes.
- Ask for side lengths as
30,20,30,20and drive them. - What happens with
notes[10] = 440? And withnotes.append(440)ten times?