One name, many things
You make a shopping list. It has milk, eggs and bread. You do not make three separate notes.
A program needs the same trick. It needs one name for many values: the scores of 30 students, or the songs in a playlist. A list does this job.
Key words
| Word | What it means | Example |
|---|---|---|
| Data type | The kind of value, such as a whole number or text. | integer, string |
| List | One name that holds many values in order. | ["milk", "eggs", "bread"] |
| Array | A list of values that are all the same data type. | [10, 20, 30] |
| Element | One value inside a list. | "eggs" |
| Index | The position number of an element. It starts at 0. | index 0 is the first |
| Append | Add an element to the end. | items.append("tea") |
| Data structure | A way to organise many values. | list, dictionary |
| Dictionary | A data structure of keys and values. | {"Sam": 16} |
The cells students fill in are left blank in this preview.
Try it: make a list
shopping = ["milk", "eggs", "bread"]
print(shopping)
print(len(shopping))
print(shopping[0])
print(shopping[2])
# len() counts the elements.
# Change shopping[2] to shopping[1] and run again.
Worked example: the index starts at 0
| Index | 0 | 1 | 2 |
|---|---|---|---|
| Element | "milk" | "eggs" | "bread" |
| Count (what a person says) | first | second | third |
The cells students fill in are left blank in this preview.
Counting starts at 0
data-callouts.warning.label
The first element is at index 0, not 1. A list with 3 elements has indexes 0, 1 and 2. Asking for index 3 gives an IndexError, because there is no fourth element.
Which index?
Have a go
Which index?
4 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.
List or array?
A list can hold mixed types, such as a name, an age and a score. An array holds values that are all the same type, such as ten scores. In many languages an array also has a fixed size.
Python has no built-in array. We use a list as an array when all the values have the same type. Both use an index.
The list commands you need
| Code | What it does | Result (each row starts with items = ["a", "b", "c"]) |
|---|---|---|
| items.append("d") | Add to the end | ["a", "b", "c", "d"] |
| items.insert(1, "x") | Put "x" at index 1, push others along | ["a", "x", "b", "c"] |
| items.remove("b") | Take out the first "b" | ["a", "c"] |
| items.pop() | Take out the last element and give it back | gives "c"; list is ["a", "b"] |
| items.index("c") | Find the index of "c" | 2 |
| items.count("a") | Count how many times "a" is there | 1 |
| "b" in items | Is it in the list? | True |
| "z" not in items | Is it missing? | True |
| items.sort() | Put the elements in order | ["a", "b", "c"] |
The cells students fill in are left blank in this preview.
Try it: the list commands
items = ["milk", "eggs"]
items.append("bread")
items.insert(0, "tea")
print(items)
items.remove("eggs")
print(items)
last = items.pop()
print(last)
print(items)
print("milk" in items)
# Predict each line before you run it.