Making lists
🌍 In real life. A shopping list is one piece of paper holding many items, in order: rice, dal, eggs, oil. You can say "the first item", "the last item", or "items 2 to 3", and you can check whether eggs are on it.
Until now, each variable held one value. A list is the shopping list of Python: one variable, many values.
The idea in one sentence: a list keeps many values in one variable, in order, and you reach each one by its index, just like the characters of a string.
Its words:
- list: values in square brackets with commas between them,
["apple", "kiwi", "fig"] - item (or element): one value in the list
- index: an item's position, starting at 0;
[-1]is the last item len(fruits): how many items there are
Step by step, for fruits[-1]:
fruitshas 3 items, at indexes 0, 1 and 2.- Negative indexes count from the end, so -1 is the last one, index 2.
fruits[-1]is'fig'.
fruits = ["apple", "kiwi", "fig"]
print(fruits)
print(len(fruits))
print(fruits[0])
print(fruits[-1])Indexes work just like they do for strings: the first item is [0], and [-1] is the last. A slice like nums[1:4] gives a new list from index 1 up to, but not including, 4.
nums = [4, 8, 15, 16, 23]
print(nums[1:4])
print(nums[:2])in checks whether a value is in the list. + joins two lists into a new one, and * repeats a list.
fruits = ["apple", "kiwi", "fig"]
print("kiwi" in fruits)
print("mango" in fruits)
print([1, 2] + [3, 4])
print([0] * 3)How Python does it, and what it costs. A list is a row of numbered slots. Python knows where slot 0 is and how big each slot is, so it can jump straight to fruits[i]: picking an item is instant, however long the list. But in has to look item by item from the front until it finds a match. On a list of a million items, a value that isn't there takes a million comparisons. (Chapter 10's sets answer in almost instantly.)
A list can mix types, [1, "two", 3.0], but most lists hold one kind of thing.
Mistakes and edge cases.
- The length as an index. A list of 2 items has indexes 0 and 1;
a[2]givesIndexError: list index out of range. - A missing comma.
["a" "b"]is['ab']: Python glues two strings that sit side by side. - in checks whole items.
"app" in ["apple"]isFalse; it isn't searching inside the words. - Adding a number to a list.
[1] + 5givesTypeError: can only concatenate list (not "int") to list. Write[1] + [5]. - Round brackets.
("a", "b")isn't a list; it's a tuple (Chapter 9).
Quiz
What does this print?
a = [10, 20, 30] print(a[1])What does this print?
a = [10, 20, 30, 40] print(a[-2])What does this print?
a = [1, 2, 3, 4, 5] print(a[1:3])What is
len([5, 7, 9, 11])?- 4
- 11
- 3
What does this print?
print(["a"] * 3)Which line has a mistake?
colors = ["red", "green", "blue"] print(colors[3])
Coding tasks
Make a list called
colorswith"red","green"and"blue", in that order. Then print the first color and the last color, each on its own line.Here is a list:
nums = [4, 8, 15, 16, 23, 42]. Print how many items it has, then print a list of its first three items.The shop sells
pets = ["cat", "dog", "fish"]. Ask for an animal. Printyesif the shop sells it, ornoif it doesn't.