Weeks
Intro to PythonWeek 4 · Lists
Week 4 Notes

Python,
Week 4

Lists: creating them, indexing and slicing, adding and removing items, and the classic list-based programs that show up again and again.

8 chapters57 worked examples~57 min read
ListsSlicingSelection sortMatrices

Chapters

  1. 01Lists: Creating, Indexing and SlicingParts 1-3, 6
  2. 02Changing a ListParts 4-5, 7-9
  3. 03Looping Through a ListPart 10
  4. 04Birthday Paradox and Naive SearchParts 11-12
  5. 05Sorting: Selection SortPart 13
  6. 06Dot Product and MatricesParts 14-16
  7. 07Try It YourselfPractice zone
  8. 08Week 4 Summary SheetAll parts
01
Lists

Lists: Creating, Indexing and Slicing

What a list is, how to reach its items, how to take slices, and how to measure its length.

1.1The Python list

List

A Python list is an ordered container. A list is created using square brackets [ ]. Objects are placed inside those brackets, separated by commas. A list can contain mixed data types.

Python
l = [1, 2, 3]
print(l)
Output
[1, 2, 3]
Python
x = ['dog', 21, True]
print(x)
Output
['dog', 21, True]
Analogy. A list is like a train: the coaches come in a fixed order, each has a seat number, and a coach can carry anything (a number, a name, a True/False). You can look at coach 3 directly without walking through coaches 1 and 2.
  • Ordered means the items keep the order you put them in.
  • Empty list (extra). [] is a list with no items. You will start many programs with it and fill it by append.
  • Mutable (extra). A list can be changed after it is made (Chapter 2): items can be replaced, added and removed.
Python
e = []
print(e)
print(len(e))
Output
[]
0

1.2Indexing

Indexing is used to access the items of a list. It uses square brackets and numbers to access individual items. 0 refers to the first item, 1 to the second item, and so on.

Python
l = [1, 2, 3]
print(l[0], l[1], l[2])
Output
1 2 3
A list: every item has a positive index (from the left) and a negative index (from the right)l =10203040500-51-42-33-24-1indexnegativel[0] is 10, l[-1] is 50, l[2] and l[-3] are the same item (30)the valid indexes are -5 to 4 for 5 items
Figure 1. A list: every item has a positive index (from the left) and a negative index (from the right)
Index starts at 0. The first item is l[0], not l[1]. A list of 3 items has the indexes 0, 1, 2, so the last valid index is len(l) - 1.

Going past the end (extra). Asking for an index that does not exist is an error:

Python
l = [1, 2, 3]
print(l[3])
Error
IndexError: list index out of range

1.3Negative indexing

Negative indexing accesses the items using negative numbers: -1 is the last item, -2 the second-to-last, and so on.

Python
l = [2, 4, 6, 8]
print(l[-1])
print(l[-2])
Output
8
6
Index0123
Positive0123
Negative-4-3-2-1
Item of [2, 4, 6, 8]2468
  • l[-1] is the quickest way to get the last item, whatever the length. l[len(l) - 1] means the same.
  • Extra. For a list of n items the valid negative indexes are -1 to -n; l[-n] is the first item. Anything beyond is an IndexError.
Python
l = [2, 4, 6, 8]
print(l[-4], l[0], l[-1] == l[len(l) - 1])
Output
2 2 True

1.4Range of indexes (slicing)

By using a colon : we can access a range of items at once. This is called slicing. Separate two indexes with the colon: the first index is the start of the range and the second is the end of the range, not included.

  • If the first index is not specified, the range starts from index 0.
  • If the last index is not specified, the range ends with the last item of the list.
Python
l = [1, 2, 3, 4, 5]
x = l[2:5]
y = l[:4]
z = l[2:]
print(x, y, z)
Output
[3, 4, 5] [1, 2, 3, 4] [3, 4, 5]
Slice l[a:b]: the dashed lines are the cut points, numbered from 0l =12345012345l[2:5][3, 4, 5]l[:4][1, 2, 3, 4]l[2:][3, 4, 5]the stop index is the cut point to the right of the last item you get
Figure 2. Slice l[a:b]: the dashed lines are the cut points, numbered from 0
Output spacing. The source shows two spaces between the lists. Python itself prints a single space between the three printed values, as in the real output above.
Slice of l = [1, 2, 3, 4, 5]ResultWhy
l[1:3][2, 3]items at index 1 and 2
l[:2][1, 2]from the start, stop before index 2
l[3:][4, 5]from index 3 to the end
l[:][1, 2, 3, 4, 5]both ends open: a copy of the whole list
l[-2:][4, 5]the last two items
l[2:2][]start equals stop: empty
l[::2][1, 3, 5]every second item (extra: a step)
l[::-1][5, 4, 3, 2, 1]reversed (extra: negative step)
Slices never raise an error. A slice that goes past the end just stops at the end: [1, 2, 3][1:10] gives [2, 3]. A slice is also a new list; slicing does not change the original.
Python
l = [1, 2, 3]
print(l[1:10])
m = l[:2]
m[0] = 99
print(m, l)
Output
[2, 3]
[99, 2] [1, 2, 3]

1.5Getting the length

To get the length, or the number of items in a list, use the len() function.

Python
l = [4, 8, 2, 1]
print(len(l))
Output
4
  • The last item is at index len(l) - 1, and a loop over every index is range(len(l)).
  • len([]) is 0; a nested list counts as one item: len([1, [2, 3]]) is 2.
Python
print(len([]), len([1, [2, 3]]))
Output
0 2

1.6Going deeper

  • Strings index and slice the same way (extra). "python"[0] is p, "python"[-1] is n, "python"[1:4] is yth. What you learn here carries over to strings.
  • Lists inside lists (extra). An item can itself be a list. m = [[1, 2], [3, 4]] has m[0] equal to [1, 2] and m[1][0] equal to 3. You will use this for matrices in Chapter 6.
Python
print("python"[0], "python"[-1], "python"[1:4])
m = [[1, 2], [3, 4]]
print(m[0], m[1][0])
Output
p n yth
[1, 2] 3

1.7Exam tips and common mistakes

Exam tips
  • Write the indexes under the list (0, 1, 2, ... and -n, ..., -1) before answering any indexing or slicing question.
  • For l[a:b] the answer has b - a items (when both are inside the list).
Common mistakes
  • Thinking the first item is l[1].
  • Including the end index in a slice: l[2:5] stops before index 5.
  • Using l[len(l)] for the last item (use l[-1] or l[len(l) - 1]).

Practice questions (graded-assignment style)

Q1Which item?

Multiple Choice Question (MCQ): choose the one correct option.

What does the program print?

Python
l = [10, 20, 30, 40, 50]
print(l[-2])
1
10
2
20
3
40
4
50
Solution
Idea

Negative indexes count from the right: -1 is the last, -2 the second last.

Python
l = [10, 20, 30, 40, 50]
print(l[-2])
Output
40
  • The list has 5 items; l[-2] is the second item from the end, which is 40.
  • Answer: option 3.
Q2A slice

Multiple Choice Question (MCQ): choose the one correct option.

What does the program print?

Python
l = [1, 2, 3, 4, 5]
print(l[1:4])
1
[2, 3, 4]
2
[3, 4, 5]
3
[2, 3, 4, 5]
4
[3, 4]
Solution
Idea

The stop index is not included.

Python
l = [1, 2, 3, 4, 5]
print(l[1:4])
Output
[2, 3, 4]
  • l[1:4] takes the items at indexes 1, 2, 3, which are 2, 3, 4.
  • Answer: option 2.
Q3How many items?

Numerical Answer Type (NAT).

How many items does l[2:7] contain when l = list(range(10))?

Python
l = list(range(10))
print(len(l[2:7]))
Solution
Idea

Stop minus start.

Python
l = list(range(10))
print(len(l[2:7]))
Output
5
  • 7 - 2 = 5 items: 2, 3, 4, 5, 6.
  • Answer: 5
Q4The last slice

Numerical Answer Type (NAT).

What is the sum of the items printed by the program?

Python
l = [10, 20, 30, 40, 50]
print(sum(l[-2:]))
Solution
Idea

l[-2:] is the last two items.

Python
l = [10, 20, 30, 40, 50]
print(sum(l[-2:]))
Output
90
  • l[-2:] is [40, 50], whose sum is 90.
  • Answer: 90
Q5An error?

Multiple Choice Question (MCQ): choose the one correct option.

What happens when this program runs?

Python
l = [1, 2, 3]
print(l[3])
1
It prints 3
2
It prints [1, 2, 3]
3
IndexError
4
It prints nothing and stops silently
Solution
Idea

Valid indexes are 0 to len(l) - 1.

Python
l = [1, 2, 3]
print(l[3])
Error
IndexError: list index out of range
  • The list has 3 items, so the valid indexes are 0, 1, 2. Index 3 does not exist.
  • Answer: option 3.
Q6List facts

Multiple Select Question (MSQ): select all that apply.

Choose the correct statements about Python lists.

1
A list is created with square brackets
2
A list can mix numbers, strings and booleans
3
The first item of a list is at index 1
4
l[-1] is the last item
Solution
Idea

Indexing starts at 0.

  • Statement 3 is false: the first item is at index 0.
  • Answer: options 1, 2 and 4.
Revision overview
Create[ ] with commas
Ordered, mixed types
[] is empty
Indexl[0] first item
l[-1] last item
Out of range: IndexError
Slicel[a:b]: b not included
Omit a: from the start
Omit b: to the end
ListsChapter 1
Lengthlen(l) = number of items
Last index: len(l) - 1
New listA slice is a new list
l[:] copies the list
ExtrasStep: l[::2]
Reverse: l[::-1]
Revision: creating, indexing, slicing and len on one page
Next · ListsChanging a List
02
Lists

Changing a List

Changing, adding, deleting and checking items, and joining lists with extend.

A list can be changed after it is created. This chapter covers changing an item, adding items (append, insert), deleting items (pop, remove, del), checking membership with in, and joining lists with extend.

2.1Changing an item's value

To change an item's value, access the index first and use the assignment operator.

Python
l = [1, 2, 8, 6, 12, 14]
l[3] = 9
print(l)
Output
[1, 2, 8, 9, 12, 14]
  • The list keeps its length: one item is replaced, nothing is added or removed.
  • Extra. A slice can be assigned too: l[1:3] = [0, 0] replaces two items at once.
  • Extra. Strings cannot be changed like this: they are immutable.
Python
l = [1, 2, 3, 4]
l[1:3] = [0, 0]
print(l)
Output
[1, 0, 0, 4]
Python
s = "cat"
s[0] = "b"
Error
TypeError: 'str' object does not support item assignment

2.2Adding items: append() and insert()

append(x)

The append() method adds an item to the end of the list.

Python
l = [2, 4, 6]
l.append(8)
print(l)
Output
[2, 4, 6, 8]
insert(i, x)

The insert() method inserts an item at a specific index. The item that was there, and all the items after it, move one place to the right.

Python
l = [2, 4, 6]
l.insert(0, 0)
l.insert(4, 8)
print(l)
Output
[0, 2, 4, 6, 8]
StepStatementList after
0l = [2, 4, 6][2, 4, 6]
1l.insert(0, 0)[0, 2, 4, 6]
2l.insert(4, 8)[0, 2, 4, 6, 8]
  • append(x) is the same as insert(len(l), x).
  • Extra. An index past the end does not fail: insert(100, x) just adds x at the end.
  • Extra. append and insert change the list and return None. Writing l = l.append(5) destroys the list.
Python
l = [1, 2]
l.insert(100, 9)
print(l)
l = l.append(5)
print(l)
Output
[1, 2, 9]
None
Before and after: the amber items are the ones that changedl.append(8)2462468l.insert(0, 0)2460246l.pop()2345234l.remove(3)2345245del l[2]2345235
Figure 3. Before and after: the amber items are the ones that changed

2.3Deleting items: pop(), remove(), del

ToolWhat it removesResult for l = [2, 3, 4, 5]
l.pop()the last item[2, 3, 4]
l.remove(3)the item with the given value (the first one)[2, 4, 5]
del l[2]the item at the given index[2, 3, 5]
Python
l = [2, 3, 4, 5]
l.pop()
print(l)
Output
[2, 3, 4]
Python
l = [2, 3, 4, 5]
l.remove(3)
print(l)
Output
[2, 4, 5]
Python
l = [2, 3, 4, 5]
del l[2]
print(l)
Output
[2, 3, 5]
Remember

remove(3) removes the value 3 (wherever it is); del l[3] removes the item at index 3. They are different.

  • Extra: pop gives the item back. x = l.pop() stores the removed item; l.pop(i) removes the item at index i.
  • Extra: remove deletes only the first match. [1, 2, 1].remove(1) leaves [2, 1]. If the value is missing it raises a ValueError.
  • Extra: del can remove a slice. del l[1:3] removes the items at indexes 1 and 2.
Python
l = [5, 6, 7]
x = l.pop()
print(x, l)
m = [1, 2, 1]
m.remove(1)
print(m)
n = [1, 2, 3, 4, 5]
del n[1:3]
print(n)
Output
7 [5, 6]
[2, 1]
[1, 4, 5]
Python
l = [1, 2, 3]
l.remove(9)
Error
ValueError: list.remove(x): x not in list

2.4Checking if an item exists: in

To check if an item exists in a list, use the in operator. It returns True or False if the item is found or not found, respectively.

Python
pets = ['dog', 'cat', 'rabbit']
print('dog' in pets)
print('python' in pets)
Output
True
False
  • x not in l is the opposite (extra).
  • The check compares with every item, so a long list takes longer (Chapter 4 explains linear search).
  • Use in before remove to avoid the ValueError (extra).
Python
l = [1, 2, 3]
x = 9
if x in l:
    l.remove(x)
else:
    print(x, "is not in the list")
print(3 not in l)
Output
9 is not in the list
False

2.5Extending a list: extend()

The extend() method adds all the items from one list to another list.

Python
l1 = [1, 2, 3]
l2 = [4, 5, 6]
l1.extend(l2)
print(l1)
Output
[1, 2, 3, 4, 5, 6]
extend adds the items; append adds the list itself as one iteml1 = [1, 2, 3] l2 = [4, 5, 6]l1.extend(l2)123456l1.append(l2)123[4, 5, 6]extend: [1, 2, 3, 4, 5, 6]append: [1, 2, 3, [4, 5, 6]] (4 items, the last one is a list)
Figure 4. extend adds the items; append adds the list itself as one item
Python
l1 = [1, 2, 3]
l2 = [4, 5, 6]
l1.append(l2)
print(l1)
print(len(l1))
l3 = [1, 2] + [3, 4]
print(l3)
Output
[1, 2, 3, [4, 5, 6]]
4
[1, 2, 3, 4]
append or extend? append(x) always adds one item. If x is a list, that list becomes a single item inside the list. To add its items one by one use extend (or +, which makes a new list).

2.6Two names for one list (extra)

b = a does not copy a list. Both names point to the same list, so a change made through one shows up through the other. To get a separate copy use a[:] (or a.copy()).

Python
a = [1, 2, 3]
b = a
b.append(4)
print(a)
c = a[:]
c.append(5)
print(a, c)
Output
[1, 2, 3, 4]
[1, 2, 3, 4] [1, 2, 3, 4, 5]
b = a gives two names for the same list; a[:] makes a separate copyab = a123one list, two namesc = a[:]123a new listchange through b and a changes too
Figure 5. b = a gives two names for the same list; a[:] makes a separate copy

2.7Going deeper

  • Which tool when (extra). You know the value: remove. You know the position: del l[i] or pop(i). You need the removed item back: pop. The last item only: pop().
  • Cost (extra). Adding at the end is cheap; adding or removing near the front shifts every later item, so it takes longer on very long lists.

2.8Exam tips and common mistakes

Exam tips
  • Redraw the list after every statement (a trace table) and watch the indexes shift after an insert or a delete.
  • append adds one item, extend adds many: check whether the argument is a list.
Common mistakes
  • l = l.append(x): the method returns None.
  • Using remove(i) when you meant to delete the item at index i (use del l[i]).
  • b = a and thinking it is a copy.
  • insert(i, x) with the arguments the wrong way round (index first, then the value).

Practice questions (graded-assignment style)

Q1insert

Multiple Choice Question (MCQ): choose the one correct option.

What does the program print?

Python
l = [2, 4, 6]
l.insert(0, 0)
l.insert(4, 8)
print(l)
1
[0, 2, 4, 6, 8]
2
[2, 4, 6, 8, 0]
3
[0, 2, 4, 8, 6]
4
[2, 0, 4, 6, 8]
Solution
Idea

insert(index, item) puts the item at the index.

Python
l = [2, 4, 6]
l.insert(0, 0)
l.insert(4, 8)
print(l)
Output
[0, 2, 4, 6, 8]
  • insert(0, 0) puts 0 at the front; insert(4, 8) puts 8 at index 4, which is the end of the 4-item list.
  • Answer: option 1.
Q2remove or del

Multiple Choice Question (MCQ): choose the one correct option.

What does the program print?

Python
l = [10, 20, 30, 40]
l.remove(30)
del l[0]
print(l)
1
[20, 40]
2
[10, 20]
3
[20, 30]
4
[10, 40]
Solution
Idea

remove(30) removes the value 30; del l[0] removes the item at index 0.

Python
l = [10, 20, 30, 40]
l.remove(30)
del l[0]
print(l)
Output
[20, 40]
  • After remove(30) the list is [10, 20, 40].
  • Then del l[0] deletes the first item, 10, leaving [20, 40].
  • Answer: option 1.
Q3Length after changes

Numerical Answer Type (NAT).

What does the program print?

Python
l = [1, 2, 3]
l.append(4)
l.pop()
l.extend([8, 9])
print(len(l))
Solution
Idea

Count what each statement adds or removes.

Python
l = [1, 2, 3]
l.append(4)
l.pop()
l.extend([8, 9])
print(len(l))
Output
5
  • It starts with 3 items. append(4) makes 4, pop() makes 3, and extend([8, 9]) adds two items to make 5.
  • Answer: 5
Q4append or extend

Multiple Choice Question (MCQ): choose the one correct option.

What does the program print?

Python
l = [1, 2, 3]
l.append([4])
print(l)
1
[1, 2, 3, 4]
2
[1, 2, 3, [4]]
3
[1, 2, 3, 4, 4]
4
None
Solution
Idea

append adds the list as one item.

Python
l = [1, 2, 3]
l.append([4])
print(l)
Output
[1, 2, 3, [4]]
  • The list [4] becomes one item, so the result is [1, 2, 3, [4]].
  • Answer: option 2.
Q5Two names

Numerical Answer Type (NAT).

What does the program print?

Python
a = [1, 2]
b = a
b.append(3)
print(len(a))
Solution
Idea

b = a shares the list.

Python
a = [1, 2]
b = a
b.append(3)
print(len(a))
Output
3
  • b and a are the same list, so the append through b is seen through a: the length is 3.
  • Answer: 3
Q6List methods

Multiple Select Question (MSQ): select all that apply.

Choose the correct statements.

1
l.pop() removes the last item
2
l.remove(x) removes the item at index x
3
del l[i] removes the item at index i
4
x in l gives True or False
Solution
Idea

remove works on values, del on positions.

  • Statement 2 is false: remove(x) removes the first item whose value is x.
  • Answer: options 1, 3 and 4.
Next · ListsLooping Through a List
03
Lists

Looping Through a List

Visiting every item, and the patterns that use it: filter, count, total, largest, new list.

Looping through a list basically means accessing all its items one by one. The for loop is used to loop through a list.

3.1The for loop over a list

Python
l = [1, 2, 3]
for i in l:
    print(i)
Output
1
2
3
for x in l visits every item once, in order; here only items above 10 are printedfor x in [3, 12, 7, 18]:3x = 3—12x = 12print7x = 7—18x = 18printthe loop variable x takes each item in turn
Figure 6. for x in l visits every item once, in order; here only items above 10 are printed
  • The loop variable (i above) is not an index: it holds the item itself, one per round.
  • The loop ends by itself after the last item. No counter and no stop test are needed.
Analogy. Think of a teacher calling the roll: she goes through the register from the first name to the last, one name at a time, and stops by herself at the end. The for loop reads a list the same way.

3.2Looping with the index

Sometimes you need the position as well as the item. Loop over the indexes with range(len(l)), or use a while loop with a counter (the same two ways Week 3 gave for numbers).

Python
l = [10, 20, 30]
for i in range(len(l)):
    print(i, l[i])
Output
0 10
1 20
2 30
Python
l = [10, 20, 30]
i = 0
while i < len(l):
    print(i, l[i])
    i += 1
Output
0 10
1 20
2 30
StyleUse when
for x in lyou only need the items
for i in range(len(l))you need the index too, or you change l[i]
while i < len(l)the step is not simply +1, or you stop early
for i, x in enumerate(l)index and item together (extra)
Python
l = [10, 20, 30]
for i, x in enumerate(l):
    print(i, x)
Output
0 10
1 20
2 30

3.3Patterns you will use again and again

Print only some items (a filter)

Python
nums = [3, 12, 7, 18, 9, 25]
for n in nums:
    if n > 10:
        print(n)
Output
12
18
25

Add up and average (an accumulator)

Python
marks = [70, 85, 90, 65]
total = 0
for m in marks:
    total += m
print(total)
print(total / len(marks))
Output
310
77.5

Find the largest item and where it is

Python
l = [7, 42, 5, 42, 9]
big = l[0]
pos = 0
for i in range(len(l)):
    if l[i] > big:
        big = l[i]
        pos = i
print(big, pos)
Output
42 1

Build a new list

Python
squares = []
for n in range(1, 6):
    squares.append(n * n)
print(squares)
Output
[1, 4, 9, 16, 25]
Shortcuts (extra). Python has built-in helpers for the first two patterns: sum(l), max(l), min(l). The loops above show how they work, and exams usually ask for the loop.
Python
l = [7, 42, 5, 42, 9]
print(sum(l), max(l), min(l))
Output
105 42 5

3.4A trap: changing a list while looping over it (extra)

Removing items from the list that a for loop is walking over makes the loop skip items, because the later items shift left under the loop's feet.

Python
l = [2, 4, 6, 1]
for x in l:
    if x % 2 == 0:
        l.remove(x)
print(l)
Output
[4, 1]

The even numbers were meant to be removed, but 4 survived: after 2 was removed, 4 moved to index 0 and the loop went on to index 1. The safe way is to build a new list of the items you want to keep:

Python
l = [2, 4, 6, 1]
odds = []
for x in l:
    if x % 2 != 0:
        odds.append(x)
print(odds)
Output
[1]

3.5Going deeper

  • Strings are loopable too (extra). for ch in "hello": visits the characters. The same loop idea works for any sequence.
  • Changing items in place (extra). To double every item, loop over the indexes: for i in range(len(l)): l[i] = l[i] * 2. Changing the loop variable x does not change the list.
Python
l = [1, 2, 3]
for x in l:
    x = x * 2
print(l)
for i in range(len(l)):
    l[i] = l[i] * 2
print(l)
Output
[1, 2, 3]
[2, 4, 6]

3.6Reading a list from input, list comprehensions, tuples and bubble sort

A. Reading a whole list from one line of input

Programs often receive numbers on one line, such as 3 1 4 1 5. input() gives that line as one string; split() cuts it into a list of strings; each piece must then be converted:

Python
line = "3 1 4"                          # what input() would return
parts = line.split()
print(parts)                           # strings
nums = []
for p in parts:
    nums.append(int(p))
print(nums)                            # integers
print(sum(nums))
print([int(p) for p in line.split()])  # the same, in one line
Output
['3', '1', '4']
[3, 1, 4]
8
[3, 1, 4]

B. List comprehensions (extra)

A list comprehension builds a list in one line: [expression for item in sequence if condition]. The if part is optional. It is exactly a loop that appends:

Python
squares = [x * x for x in range(5)]
print(squares)

evens_squared = [x * x for x in range(5) if x % 2 == 0]
print(evens_squared)

# the equivalent loop for the third list
multiples = []
for x in range(1, 10):
    if x % 3 == 0:
        multiples.append(x)
print(multiples)

print(len([x for x in range(20) if x % 3 == 0]))   # counting with a comprehension
Output
[0, 1, 4, 9, 16]
[0, 4, 16]
[3, 6, 9]
7
  • Read it as: "for each x in the sequence, if the condition holds, put expression in the list".
  • The if filters; the expression at the front transforms. [x * x for x in range(5) if x % 2 == 0] keeps only the even x, then squares them.
  • The original sequence is not changed. Use a normal loop when the logic needs several statements.

C. Tuples and swapping

  • A tuple is like a list written with round brackets, but it cannot be changed after it is created. t = (1, 2, 3), then t[0] = 9 raises a TypeError. A tuple with one item needs a comma: (5,).
  • Tuples unpack: x, y = (3, 4). This is also how a swap works: a, b = b, a builds the tuple (b, a) first, then assigns, so no temporary variable is needed. The same trick swaps list items: l[i], l[j] = l[j], l[i].
Python
t = (1, 2, 3)
t[0] = 9
Output
TypeError: 'tuple' object does not support item assignment
Python
x, y = (3, 4)
print(x, y)
x, y = y, x                    # swap
print(x, y)
l = [1, 2, 3]
l[0], l[2] = l[2], l[0]        # swap two list items
print(l)
print((5,))                    # a one-item tuple needs the comma
Output
3 4
4 3
[3, 2, 1]
(5,)

D. sort() changes the list and returns None

l.sort() sorts the list in place and returns None. sorted(l) leaves l alone and returns a new sorted list. The slip l = l.sort() destroys the list:

Python
l = [3, 1, 2]
print(sorted(l))               # a new list
print(l)                       # l itself is unchanged
print(l.sort())                # sort returns None
print(l)                       # ... but l is now sorted
Output
[1, 2, 3]
[3, 1, 2]
None
[1, 2, 3]

E. Bubble sort

Selection sort picks the smallest item for each position. Bubble sort instead walks along the list and swaps any neighbouring pair that is out of order. After one full pass the largest item has "bubbled" to the end, so each pass can stop one place earlier.

Python
l = [5, 1, 4, 2]
n = len(l)

# one pass only
for i in range(n - 1):
    if l[i] > l[i + 1]:
        l[i], l[i + 1] = l[i + 1], l[i]
print(l)                       # 5 has bubbled to the end

# the full algorithm
for p in range(n - 1):
    for i in range(n - 1 - p):
        if l[i] > l[i + 1]:
            l[i], l[i + 1] = l[i + 1], l[i]
print(l)
Output
[1, 4, 2, 5]
[1, 2, 4, 5]
Step (first pass on [5, 1, 4, 2])CompareList after
i = 05 > 1, swap[1, 5, 4, 2]
i = 15 > 4, swap[1, 4, 5, 2]
i = 25 > 2, swap[1, 4, 2, 5]
  • A list of nn items needs at most n−1n - 1 passes. Both bubble sort and selection sort make about n2/2n^2/2 comparisons, so both are slow for long lists. Python's own sort() is much faster.
  • Index i + 1 is why the inner loop stops at n - 1 - p: asking for l[i + 1] past the last item would raise IndexError.

3.7Exam tips and common mistakes

Exam tips
  • Name the pattern first: filter, count, total, largest, or build a new list. The loop shape follows from it.
  • Start accumulators (total = 0, count = 0) before the loop, never inside it.
Common mistakes
  • Using the loop variable as if it were an index: for i in l: print(l[i]).
  • Resetting total = 0 inside the loop.
  • Removing items from the list you are looping over.

Practice questions (graded-assignment style)

Q1What is printed?

Multiple Choice Question (MCQ): choose the one correct option.

What does the program print?

Python
nums = [3, 12, 7, 18, 9, 25]
for n in nums:
    if n > 10:
        print(n)
1
12, 18, 25 (one per line)
2
All six items
3
Only 12
4
Only 25
Solution
Idea

The if filters the items.

Python
nums = [3, 12, 7, 18, 9, 25]
for n in nums:
    if n > 10:
        print(n)
Output
12
18
25
  • Only the items greater than 10 are printed, one per line: 12, 18, 25.
  • Answer: option 1.
Q2The total

Numerical Answer Type (NAT).

What does the program print?

Python
marks = [70, 85, 90, 65]
total = 0
for m in marks:
    total += m
print(total)
Solution
Idea

Add each mark to total.

Python
marks = [70, 85, 90, 65]
total = 0
for m in marks:
    total += m
print(total)
Output
310
  • 70 + 85 + 90 + 65 = 310.
  • Answer: 310
Q3Where is the biggest?

Numerical Answer Type (NAT).

What index does the program print?

Python
l = [7, 42, 5, 42, 9]
big = l[0]
pos = 0
for i in range(len(l)):
    if l[i] > big:
        big = l[i]
        pos = i
print(pos)
Solution
Idea

The test is >, so a later equal value does not replace it.

Python
l = [7, 42, 5, 42, 9]
big = l[0]
pos = 0
for i in range(len(l)):
    if l[i] > big:
        big = l[i]
        pos = i
print(pos)
Output
1
  • The first 42 is at index 1; the second 42 is not greater than it, so the position stays 1.
  • Answer: 1
Q4Does the list change?

Multiple Choice Question (MCQ): choose the one correct option.

What does the program print?

Python
l = [1, 2, 3]
for x in l:
    x = x * 2
print(l)
1
[2, 4, 6]
2
[1, 2, 3]
3
[1, 4, 6]
4
An error
Solution
Idea

x = x * 2 rebinds the loop variable only.

Python
l = [1, 2, 3]
for x in l:
    x = x * 2
print(l)
Output
[1, 2, 3]
  • The loop variable x is a copy of each item. Doubling it does not touch the list, which stays [1, 2, 3].
  • Answer: option 2.
Q5Skipped items

Numerical Answer Type (NAT).

How many items are left in the list when the program ends?

Python
l = [2, 4, 6, 1]
for x in l:
    if x % 2 == 0:
        l.remove(x)
print(len(l))
Solution
Idea

Removing during the loop makes it skip.

Python
l = [2, 4, 6, 1]
for x in l:
    if x % 2 == 0:
        l.remove(x)
print(len(l))
Output
2
  • The program prints [4, 1]: two items are left, because 4 was skipped after 2 was removed.
  • Answer: 2
Q6Looping facts

Multiple Select Question (MSQ): select all that apply.

Choose the correct statements.

1
for x in l gives each item, not its index
2
range(len(l)) gives the indexes of l
3
Changing x inside for x in l changes the list
4
Start a total before the loop
Solution
Idea

The loop variable is a name for the current item.

  • Statement 3 is false: only l[i] = ... changes the list.
  • Answer: options 1, 2 and 4.
Q1A filtering comprehension

Multiple Choice Question (MCQ): choose the one correct option.

What does the program print?

Python
print([x * x for x in range(6) if x % 2 == 1])
1
[1, 9, 25]
2
[0, 4, 16]
3
[1, 3, 5]
4
[1, 4, 9, 16, 25]
Solution
Idea

Filter first (if), then transform (the expression at the front).

Python
print([x * x for x in range(6) if x % 2 == 1])
Output
[1, 9, 25]
  • range(6) is 0..5. The odd values are 1, 3, 5; squared they are 1, 9, 25.
  • Option 3 forgets to square. Option 2 uses the even values.
  • Answer: option 1.
Q2Counting with a comprehension

Numerical Answer Type (NAT).

What does the program print?

Python
print(len([x for x in range(20) if x % 3 == 0]))
Solution
Idea

The comprehension collects the multiples of 3 below 20; len counts them.

Python
print(len([x for x in range(20) if x % 3 == 0]))
Output
7
  • The list is [0, 3, 6, 9, 12, 15, 18], which has 7 items (note that 0 counts).
  • Answer: 7
Q3sort versus sorted

Multiple Select Question (MSQ): select all that apply.

With l = [3, 1, 2], select all the true statements.

1
print(sorted(l)) shows [1, 2, 3] and leaves l as [3, 1, 2].
2
print(l.sort()) shows [1, 2, 3].
3
l = l.sort() makes l equal to None.
4
After l.sort(), the list l is [1, 2, 3].
Solution
Idea

In-place methods return None; sorted() returns a new list.

Python
l = [3, 1, 2]
print(sorted(l), l)
print(l.sort())
print(l)
l = l.sort()
print(l)
Output
[1, 2, 3] [3, 1, 2]
None
[1, 2, 3]
None
  • 1 ✓. 2 ✗ it prints None. 3 ✓. 4 ✓.
  • Answer: options 1, 3 and 4.
Q4Changing a tuple

Multiple Choice Question (MCQ): choose the one correct option.

What happens when this program runs?

Python
t = (4, 5, 6)
t[1] = 0
print(t)
1
It prints (4, 0, 6).
2
It prints (4, 5, 6).
3
It raises a TypeError.
4
It raises an IndexError.
Solution
Idea

Tuples cannot be changed after they are created.

Python
t = (4, 5, 6)
t[1] = 0
print(t)
Output
TypeError: 'tuple' object does not support item assignment
  • Index 1 exists, so it is not an IndexError; the assignment itself is not allowed. With a list ([4, 5, 6]) the same code would print [4, 0, 6].
  • Answer: option 3.
Q5One pass of bubble sort

Multiple Choice Question (MCQ): choose the one correct option.

What does the program print?

Python
l = [4, 3, 1, 2]
for i in range(len(l) - 1):
    if l[i] > l[i + 1]:
        l[i], l[i + 1] = l[i + 1], l[i]
print(l)
1
[1, 2, 3, 4]
2
[3, 1, 2, 4]
3
[3, 4, 1, 2]
4
[1, 2, 4, 3]
Solution
Idea

A single pass bubbles the largest item to the end but does not fully sort.

Python
l = [4, 3, 1, 2]
for i in range(len(l) - 1):
    if l[i] > l[i + 1]:
        l[i], l[i + 1] = l[i + 1], l[i]
print(l)
Output
[3, 1, 2, 4]
  • i=0i = 0: 4>34 > 3, swap, giving [3, 4, 1, 2]. i=1i = 1: 4>14 > 1, swap, [3, 1, 4, 2]. i=2i = 2: 4>24 > 2, swap, [3, 1, 2, 4].
  • The 4 is at the end, but the rest still needs more passes.
  • Answer: option 2.
Revision overview
for x in lEach item in order
Ends by itself
x is the item
With the indexrange(len(l))
while with a counter
enumerate (extra)
PatternsFilter, count, total
Largest and its place
Build a new list
LoopingChapter 3
TotalsStart at 0 before the loop
sum(l), max(l), min(l)
CarefulDo not remove while looping
x = x * 2 does not change l
Change in placefor i in range(len(l)):
l[i] = ...
Revision: looping through a list on one page
Next · PracticeBirthday Paradox and Naive Search
04
Practice

Birthday Paradox and Naive Search

Two classic programs: finding a repeat among random numbers, and a linear search.

Two classic programs that use lists of random numbers: the birthday paradox, and a search through a big list. Both show why lists and loops are so powerful.

4.1Practice: the birthday paradox

Generate 30 random "birthdays" (a number between 1 and 365), sort them, then check whether any two are the same.

Python
import random
l = []
# create an empty list
for i in range(30):
    l.append(random.randint(1, 365))
    # append a random number from 1 to 365, 30 times
l.sort()
print(l)
i = 0
flag = 0
while i < len(l) - 1:
    if l[i] == l[i + 1]:
        print("Repeat", l[i])
        flag = 1
        break
    i += 1
if flag == 0:
    print("There is no repetition")
  1. l = [] starts an empty list, and 30 rounds of l.append(random.randint(1, 365)) fill it with random numbers.
  2. l.sort() puts the numbers in order. Now equal numbers are neighbours, so we never have to compare every pair: only l[i] with l[i + 1].
  3. The while loop stops at len(l) - 1 because it looks one place ahead. On the first repeat it prints it, sets flag = 1 and leaves with break.
  4. If flag is still 0 after the loop, no repeat was found.
Sorted birthdays: a repeat must be two neighbours148787120233301≠=≠≠≠after l.sort() equal numbers sit side by sideso compare only l[i] with l[i + 1]here the pair at positions 1 and 2 is the repeat (87)
Figure 7. Sorted birthdays: a repeat must be two neighbours
The numbers change every run. The program uses random numbers, so every run prints a different list. To make the examples below repeatable, a random.seed(...) line is added after the import. A seed fixes the "random" sequence; the logic is the same.

A run with a repeat

Python
import random
random.seed(2)
l = []
# create an empty list
for i in range(30):
    l.append(random.randint(1, 365))
    # append a random number from 1 to 365, 30 times
l.sort()
print(l)
i = 0
flag = 0
while i < len(l) - 1:
    if l[i] == l[i + 1]:
        print("Repeat", l[i])
        flag = 1
        break
    i += 1
if flag == 0:
    print("There is no repetition")
Output
[15, 19, 19, 29, 44, 47, 82, 87, 109, 129, 138, 158, 164, 185, 187, 191, 195, 202, 221, 228, 239, 258, 261, 279, 298, 311, 311, 327, 343, 349]
Repeat 19

A run with no repeat

Python
import random
random.seed(1)
l = []
# create an empty list
for i in range(30):
    l.append(random.randint(1, 365))
    # append a random number from 1 to 365, 30 times
l.sort()
print(l)
i = 0
flag = 0
while i < len(l) - 1:
    if l[i] == l[i + 1]:
        print("Repeat", l[i])
        flag = 1
        break
    i += 1
if flag == 0:
    print("There is no repetition")
Output
[2, 12, 14, 15, 16, 33, 49, 53, 61, 69, 108, 118, 131, 137, 163, 195, 200, 222, 229, 231, 242, 250, 254, 278, 292, 303, 312, 333, 334, 357]
There is no repetition

Why "paradox"? With only 30 people, a repeat sounds unlikely out of 365 days, but the chance is about 70.6%. With just 23 people it is already above 50% (about 50.7%). The reason is that 30 people make 30 × 29 / 2 = 435 pairs, and any one pair could match.

Check by simulation (extra). Repeat the experiment 10,000 times and count how often a repeat appears:

Python
import random
random.seed(1)
count = 0
for t in range(10000):
    l = []
    for i in range(30):
        l.append(random.randint(1, 365))
    if len(set(l)) < 30:
        count += 1
print(count / 10000)
Output
0.7034
The simulated fraction is 0.7034, close to the exact 0.706. The check len(set(l)) < 30 asks "are there fewer different numbers than items?", which is another way to detect a repeat without sorting.

4.2Practice: naive search in a list

Build a list of 100,000 random numbers, then repeatedly ask the user for a number and search for it by scanning the whole list.

Python
import random as r
l = []
for i in range(100000):
    l.append(r.randint(1, 1000000))
n = 0
while n > -1:
    print("Enter a no., type -1 to exit")
    n = int(input())
    flag = 0
    for i in range(len(l)):
        if n == l[i]:
            print("Found")
            flag = 1
            break
    if flag == 0:
        print("Not found")
Linear search: look at the items one by one from the frontsearching for 2541#17#293#325#46018773found after 4 looksthe red items were checked and did not match; the grey ones were never looked atworst case: all the items are checked (100,000 for a list that size)
Figure 8. Linear search: look at the items one by one from the front

This is a linear search: it checks every element one by one, so it can take up to 100,000 comparisons for a list of that size. When the number is found, break stops the scan early; when it is not found, the whole list has been checked.

A run with the seed fixed (the number typed first is an item that is in the list, then 0 which never is, then -1 to exit):

Python
import random as r
r.seed(5)
l = []
for i in range(100000):
    l.append(r.randint(1, 1000000))
n = 0
while n > -1:
    print("Enter a no., type -1 to exit")
    n = int(input())
    flag = 0
    for i in range(len(l)):
        if n == l[i]:
            print("Found")
            flag = 1
            break
    if flag == 0:
        print("Not found")
Output
Enter a no., type -1 to exit
400109
Found
Enter a no., type -1 to exit
0
Not found
Enter a no., type -1 to exit
-1
Not found
A small quirk. The loop condition is n > -1, so the -1 typed to exit is still searched first, and the program prints Not found once more before it stops. Any negative number does the same. It is harmless, but worth knowing.
Example 7Count the looks (extra)

Search for 25 in [41, 7, 93, 25, 60, 18, 77, 3] and count how many items are looked at.

Solution
Python
l = [41, 7, 93, 25, 60, 18, 77, 3]
n = 25
count = 0
for x in l:
    count += 1
    if x == n:
        break
print("looks:", count)
Output
looks: 4

Items 41, 7 and 93 do not match, and the fourth item is the match, so 4 looks. If the number were absent, all 8 items would be looked at. For a list of n items that is n looks in the worst case, and about n/2 on average when the number is present.

  • The in operator searches too (extra). n in l does the same linear scan for you in one short line.
  • Faster search needs order (extra). If the list is sorted, much faster methods exist that skip most of the items. That is a topic for later; here the point is the cost of the plain scan.

4.3Exam tips and common mistakes

Exam tips
  • Sorting first makes the duplicate check easy: only neighbours need comparing.
  • Use a flag (0/1) when a loop must report whether something was found, and test it after the loop.
Common mistakes
  • Running the neighbour loop up to len(l) instead of len(l) - 1 (l[i + 1] then goes out of range).
  • Printing "Not found" inside the scan loop instead of after it: it would print for every non-matching item.
  • Forgetting flag = 0 before each new search.

Practice questions (graded-assignment style)

Q1Why sort first?

Multiple Choice Question (MCQ): choose the one correct option.

Why does the birthday program call l.sort() before checking for a repeat?

1
So that equal numbers become neighbours
2
So that the list gets shorter
3
Because append needs a sorted list
4
To remove the repeats
Solution
Idea

Think about which pairs you still have to compare.

  • After sorting, equal values sit side by side, so only l[i] against l[i + 1] has to be checked.
  • Answer: option 1.
Q2Neighbour check

Numerical Answer Type (NAT).

What does the program print?

Python
l = [233, 87, 14, 301, 87, 120]
l.sort()
i = 0
while i < len(l) - 1:
    if l[i] == l[i + 1]:
        print(l[i])
        break
    i += 1
Solution
Idea

Find the first neighbouring equal pair.

Python
l = [233, 87, 14, 301, 87, 120]
l.sort()
i = 0
while i < len(l) - 1:
    if l[i] == l[i + 1]:
        print(l[i])
        break
    i += 1
Output
87
  • After sorting the list is [14, 87, 87, 120, 233, 301]; the first equal neighbours are the two 87s.
  • Answer: 87
Q3Counting the looks

Numerical Answer Type (NAT).

How many items does the loop look at before it stops?

Python
l = [41, 7, 93, 25, 60, 18, 77, 3]
count = 0
for x in l:
    count += 1
    if x == 25:
        break
print(count)
Solution
Idea

The loop stops at the first match.

Python
l = [41, 7, 93, 25, 60, 18, 77, 3]
count = 0
for x in l:
    count += 1
    if x == 25:
        break
print(count)
Output
4
  • It looks at 41, 7, 93 and then 25, the match, so 4 items.
  • Answer: 4
Q4Worst case

Multiple Choice Question (MCQ): choose the one correct option.

A linear search through a list of 100,000 items looks for a number that is not in the list. How many items are compared?

1
1
2
About 50,000
3
100,000
4
1,000,000
Solution
Idea

When the number is absent the scan never stops early.

  • Every one of the 100,000 items is compared and none matches.
  • Answer: option 3.
Q5Pairs of 30 people

Numerical Answer Type (NAT).

How many different pairs can be formed from 30 people? (Use 30 × 29 / 2.)

Solution
Idea

Pairs count each two people once.

  • 30 × 29 / 2 = 435.
  • Answer: 435
Q6Search facts

Multiple Select Question (MSQ): select all that apply.

Choose the correct statements about the naive search.

1
It checks the items one by one from the front
2
break stops the scan after a match
3
It needs the list to be sorted
4
In the worst case it checks every item
Solution
Idea

It works on any list.

  • Statement 3 is false: a linear search needs no order.
  • Answer: options 1, 2 and 4.
Revision overview
Birthday paradox30 random numbers 1..365
sort, then compare neighbours
flag and break
Probability30 people: about 70.6%
23 people: about 50.7%
435 pairs
Naive searchScan from the front
break when found
flag 0 means not found
Practice 1Chapter 4
CostLinear search: up to n looks
100,000 for this list
Quirk-1 is searched before exit
so "Not found" prints once more
Other wayslen(set(l)) < len(l)
n in l
Revision: the birthday paradox and linear search on one page
Next · PracticeSorting: Selection Sort
05
Practice

Sorting: Selection Sort

Sorting by repeatedly moving the smallest item, with a full trace.

Sorting means putting the items in order. Selection sort is the simplest idea: repeatedly find the smallest remaining item, move it into a new list, and remove it from the old one.

5.1The program

Python
l = [12, 10, 7, 18, 6, 42, 8, 5, 35, 7]
x = []
while len(l) > 0:
    minVal = l[0]
    for i in range(len(l)):
        if l[i] < minVal:
            minVal = l[i]
    x.append(minVal)
    l.remove(minVal)
print(x)
Output
[5, 6, 7, 7, 8, 10, 12, 18, 35, 42]
  1. x is the new, empty list that will hold the sorted result. The outer while runs as long as l still has items.
  2. minVal = l[0] assumes the first item is the smallest. The for loop compares every item and keeps the smaller one.
  3. x.append(minVal) moves that smallest value to the result, and l.remove(minVal) takes one copy of it out of l.
  4. Each round l gets one item shorter and x one item longer, until l is empty.
Selection sort on [12, 10, 7, 18, 6]: find the smallest, move it, remove itRound 1l12107186minVal = 6x.append(6)x = [6]Round 2l1210718minVal = 7x.append(7)x = [6, 7]Round 3l121018minVal = 10x.append(10)x = [6, 7, 10]each round shrinks l by one and grows x by one
Figure 9. Selection sort on [12, 10, 7, 18, 6]: find the smallest, move it, remove it

5.2Trace of the whole run

Roundl at the start of the roundminValx after the round
1[12, 10, 7, 18, 6, 42, 8, 5, 35, 7]5[5]
2[12, 10, 7, 18, 6, 42, 8, 35, 7]6[5, 6]
3[12, 10, 7, 18, 42, 8, 35, 7]7[5, 6, 7]
4[12, 10, 18, 42, 8, 35, 7]7[5, 6, 7, 7]
5[12, 10, 18, 42, 8, 35]8[5, 6, 7, 7, 8]
6[12, 10, 18, 42, 35]10[5, 6, 7, 7, 8, 10]
7[12, 18, 42, 35]12[5, 6, 7, 7, 8, 10, 12]
8[18, 42, 35]18[5, 6, 7, 7, 8, 10, 12, 18]
9[42, 35]35[5, 6, 7, 7, 8, 10, 12, 18, 35]
10[42]42[5, 6, 7, 7, 8, 10, 12, 18, 35, 42]
  • Duplicates work. The value 7 appears twice. remove(7) removes only one copy per round, so the second 7 is found in a later round and both reach x.
  • The original list is used up. At the end l is empty. If you need the original later, work on a copy (l[:]).

How much work? Round 1 compares 10 items, round 2 compares 9, and so on: 10 + 9 + ... + 1 = 55 comparisons (counted in the code below). For n items it is about n²/2, so doubling the list roughly quadruples the work.

Python
l = [12, 10, 7, 18, 6, 42, 8, 5, 35, 7]
comparisons = 0
x = []
while len(l) > 0:
    minVal = l[0]
    for i in range(len(l)):
        comparisons += 1
        if l[i] < minVal:
            minVal = l[i]
    x.append(minVal)
    l.remove(minVal)
print(comparisons)
Output
55

5.3Variations

Largest first (descending)

Python
l = [33, 7, 19, 2, 41, 15, 8, 27]
x = []
while len(l) > 0:
    maxVal = l[0]
    for i in range(len(l)):
        if l[i] > maxVal:
            maxVal = l[i]
    x.append(maxVal)
    l.remove(maxVal)
print(x)
Output
[41, 33, 27, 19, 15, 8, 7, 2]

Only two things changed: min became max and the test < became >.

The built-in way (extra)

Python
l = [12, 10, 7, 18, 6, 42, 8, 5, 35, 7]
print(sorted(l))
print(sorted(l, reverse=True))
l.sort()
print(l)
print(l.sort())
Output
[5, 6, 7, 7, 8, 10, 12, 18, 35, 42]
[42, 35, 18, 12, 10, 8, 7, 7, 6, 5]
[5, 6, 7, 7, 8, 10, 12, 18, 35, 42]
None
sorted or sort? sorted(l) returns a new sorted list and leaves l alone. l.sort() sorts l in place and returns None, so print(l.sort()) prints None. Writing the sort yourself, as above, shows how the idea works, and exams ask for it.

Selection sort inside one list with swaps (extra)

Python
l = [12, 10, 7, 18, 6]
for start in range(len(l)):
    m = start
    for i in range(start + 1, len(l)):
        if l[i] < l[m]:
            m = i
    l[start], l[m] = l[m], l[start]
print(l)
Output
[6, 7, 10, 12, 18]

This version keeps the sorted part at the front of the same list and swaps the smallest item into place, so no second list is needed. The idea (find the smallest, put it first) is exactly the same.

5.4Going deeper

  • Why start with l[0] (extra). The starting guess must be a real item of the list. Starting with a fixed number like 0 would fail when every item is bigger.
  • Same shape as "find the maximum" (extra). The inner loop is the maximum-finding loop of Chapter 3 with the test flipped.

5.5Exam tips and common mistakes

Exam tips
  • Trace with two columns: what is left in l, and what has been collected in x.
  • For descending order flip both the name and the comparison.
Common mistakes
  • Forgetting l.remove(minVal): the loop then never ends.
  • Setting minVal = 0 at the start.
  • Printing l at the end: it is empty; the answer is in x.

Practice questions (graded-assignment style)

Q1Sorted output

Multiple Choice Question (MCQ): choose the one correct option.

What does the program print?

Python
l = [5, 9, 3, 1]
x = []
while len(l) > 0:
    minVal = l[0]
    for i in range(len(l)):
        if l[i] < minVal:
            minVal = l[i]
    x.append(minVal)
    l.remove(minVal)
print(x)
1
[1, 3, 5, 9]
2
[9, 5, 3, 1]
3
[5, 9, 3, 1]
4
[]
Solution
Idea

Each round moves the smallest remaining item.

Python
l = [5, 9, 3, 1]
x = []
while len(l) > 0:
    minVal = l[0]
    for i in range(len(l)):
        if l[i] < minVal:
            minVal = l[i]
    x.append(minVal)
    l.remove(minVal)
print(x)
Output
[1, 3, 5, 9]
  • The smallest values are moved one by one into x: 1, 3, 5, 9.
  • Answer: option 1.
Q2Items left in l

Numerical Answer Type (NAT).

What does the program print?

Python
l = [5, 9, 3, 1]
x = []
while len(l) > 0:
    minVal = l[0]
    for i in range(len(l)):
        if l[i] < minVal:
            minVal = l[i]
    x.append(minVal)
    l.remove(minVal)
print(len(l))
Solution
Idea

The loop runs while l has items.

Python
l = [5, 9, 3, 1]
x = []
while len(l) > 0:
    minVal = l[0]
    for i in range(len(l)):
        if l[i] < minVal:
            minVal = l[i]
    x.append(minVal)
    l.remove(minVal)
print(len(l))
Output
0
  • The loop only stops when l is empty, so len(l) is 0.
  • Answer: 0
Q3Comparisons

Numerical Answer Type (NAT).

For a list of 4 items this selection sort compares 4 + 3 + 2 + 1 items in total. What is the total?

Solution
Idea

Add the four numbers.

  • 4 + 3 + 2 + 1 = 10.
  • Answer: 10
Q4Which sort call?

Multiple Choice Question (MCQ): choose the one correct option.

What does the program print?

Python
l = [3, 1, 2]
print(l.sort())
1
[1, 2, 3]
2
None
3
[3, 1, 2]
4
An error
Solution
Idea

sort() sorts in place and returns None.

Python
l = [3, 1, 2]
print(l.sort())
Output
None
  • l.sort() returns None, and that is what gets printed.
  • Answer: option 2.
Q5Descending

Multiple Choice Question (MCQ): choose the one correct option.

To sort in descending order with the same program, which two changes are needed?

1
Change < to > and track the largest value
2
Change append to insert
3
Change while to for
4
Remove l.remove
Solution
Idea

Look at the descending program of this chapter.

  • Track the largest value (maxVal) and use > in the comparison.
  • Answer: option 1.
Q6Selection sort facts

Multiple Select Question (MSQ): select all that apply.

Choose the correct statements.

1
It repeatedly finds the smallest remaining item
2
It works with repeated values
3
It leaves the original list unchanged
4
The total comparisons grow roughly as n squared
Solution
Idea

The program removes items from l.

  • Statement 3 is false: the program empties the original list.
  • Answer: options 1, 2 and 4.
Revision overview
IdeaFind the smallest
move it to x
remove it from l
CodeminVal = l[0]
for loop compares
append then remove
Resultx holds the sorted list
l ends up empty
duplicates are fine
Selection sortChapter 5
Descendingmax instead of min
> instead of <
Work10 + 9 + ... + 1 = 55
about n²/2
Built-insorted(l) new list
l.sort() in place, None
Revision: selection sort on one page
Next · PracticeDot Product and Matrices
06
Practice

Dot Product and Matrices

A dot product, then matrix addition and multiplication with loops.

Lists of numbers become vectors, and lists of lists become matrices. This chapter does a dot product, then adds and multiplies matrices from first principles, using nothing but loops and indexing.

6.1Dot product

The dot product of two equal-length lists is the sum of the products of their corresponding elements.

Python
x = [1, 7, 3, 4]
y = [8, 6, 3, 2]
if len(x) == len(y):
    dot_product = 0
    for i in range(len(x)):
        dot_product += x[i] * y[i]
    print(dot_product)
else:
    print("Dot product can't be calculated")
Output
67
ix[i]y[i]x[i] * y[i]dot_product after
01888
1764250
233959
342867

The if len(x) == len(y) test checks that the lists are the same length; otherwise there is no dot product, and the else message prints:

Python
x = [1, 7, 3, 4]
y = [8, 6, 3]
if len(x) == len(y):
    dot_product = 0
    for i in range(len(x)):
        dot_product += x[i] * y[i]
    print(dot_product)
else:
    print("Dot product can't be calculated")
Output
Dot product can't be calculated
Indentation. In the source the print(dot_product) line has lost its indentation when copied. It belongs inside the if (and after the loop), as written above; otherwise the else would not match.
Python
x = [1, 7, 3, 4]
y = [8, 6, 3, 2]
print(sum(a * c for a, c in zip(x, y)))
Output
67

The last line is a one-line version using zip and sum (extra). Learn the loop version first: it is what the exam asks for.

6.2A matrix as a list of lists

A matrix here is just a list of row-lists. M[i][j] is the item in row i, column j (both counted from 0).

Python
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(A[0])
print(A[1][2])
print(A[2][0])
print(len(A), len(A[0]))
Output
[1, 2, 3]
6
7
3 3
ExpressionMeaningValue
A[0]the whole first row[1, 2, 3]
A[1][2]row 1, column 26
A[2][0]row 2, column 07

6.3Matrix addition from first principles

Two same-sized matrices are added element by element: C[i][j] = A[i][j] + B[i][j]. Two nested loops visit every (i, j).

Python
dim = 3
r1 = [1, 2, 3]
r2 = [4, 5, 6]
r3 = [7, 8, 9]
s1 = [1, 2, 1]
s2 = [6, 2, 3]
s3 = [4, 2, 1]
A = []
A.append(r1)
A.append(r2)
A.append(r3)
B = []
B.append(s1)
B.append(s2)
B.append(s3)
print(A)
print(B)
# we need to add A and B
C = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
for i in range(dim):
    for j in range(dim):
        C[i][j] = A[i][j] + B[i][j]
print(C)
Output
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[1, 2, 1], [6, 2, 3], [4, 2, 1]]
[[2, 4, 4], [10, 7, 9], [11, 10, 10]]
Output spacing. The source shows the lists without spaces after the commas. Python prints a space after each comma, as in the real output above.
  • C = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] makes a result matrix of zeros first, so that C[i][j] exists to be assigned.
  • dim = 3 is the number of rows and of columns; both loops use range(dim).

6.4Matrix multiplication

C[i][j] is the dot product of row i of A and column j of B. That needs a third nested loop, over k, that walks along the row and down the column together.

Python
r1 = [1, 2, 3]
r2 = [4, 5, 6]
r3 = [7, 8, 9]
s1 = [1, 2, 1]
s2 = [6, 2, 3]
s3 = [4, 2, 1]
A = []
B = []
A.append(r1)
A.append(r2)
A.append(r3)
B.append(s1)
B.append(s2)
B.append(s3)
C = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
dim = 3
for i in range(dim):
    for j in range(dim):
        for k in range(dim):
            C[i][j] += A[i][k] * B[k][j]
print(C)
Output
[[25, 12, 10], [58, 30, 25], [91, 48, 40]]
Matrix multiplication: one cell of C comes from a row of A and a column of BA123456789×B121623421=C251210583025914840C[2][1] = 7·2 + 8·2 + 9·2 = 14 + 16 + 18 = 48i picks the row of A, j picks the column of B, k runs along both3 x 3 times 3 x 3 needs 3 x 3 x 3 = 27 multiplications
Figure 10. Matrix multiplication: one cell of C comes from a row of A and a column of B
Correction (extra). The source comment says "C[2][1] is the dot product of the 2nd row of A and the first column of B". With Python's 0-based indexes that is not right: C[2][1] uses row index 2 of A (the third row, 7, 8, 9) and column index 1 of B (the second column, 2, 2, 2): 7·2 + 8·2 + 9·2 = 48, which is the value printed at C[2][1]. The 2nd row and first column of the comment would be C[1][0] = 4·1 + 5·6 + 6·4 = 58.
ijC[i][j] = sum over k of A[i][k] * B[k][j]value
001·1 + 2·6 + 3·425
011·2 + 2·2 + 3·212
021·1 + 2·3 + 3·110
217·2 + 8·2 + 9·248

How much work? The three nested loops multiply 3 × 3 × 3 = 27 pairs. For n × n matrices that is n³ multiplications, which is why multiplying big matrices is slow.

6.5The numpy shortcut

Python
import numpy
X = numpy.mat(A)
Y = numpy.mat(B)
print(X * Y)

The source shows this output:

Python
print("[[25 12 10]\n [58 30 25]\n [91 48 40]]")
Output
[[25 12 10]
 [58 30 25]
 [91 48 40]]
Not run here. The numpy block is shown but was not run for these notes (numpy is not installed in the checking environment). The numbers in it are the same as the loop version above, which was run and checked. Also, numpy.mat may be missing in newer numpy releases; if you get an error, use numpy.array with the @ operator (X @ Y) instead.

A three-line numpy version replaces the whole triple-nested loop. It is worth remembering once you are comfortable with the first-principles version.

6.6Going deeper

  • Addition needs equal sizes; multiplication needs matching inner sizes (extra). A (m × n) can be multiplied by B (n × p), giving m × p. The number of columns of A must equal the number of rows of B.
  • A common trap (extra). [[0] * 3] * 3 builds three rows that are the same list, so changing one row changes all of them. Write the zero matrix out, or use a loop that builds each row separately.
Python
bad = [[0] * 3] * 3
bad[0][0] = 5
print(bad)
good = []
for i in range(3):
    good.append([0] * 3)
good[0][0] = 5
print(good)
Output
[[5, 0, 0], [5, 0, 0], [5, 0, 0]]
[[5, 0, 0], [0, 0, 0], [0, 0, 0]]

6.7Exam tips and common mistakes

Exam tips
  • Addition: two loops (i, j). Multiplication: three loops (i, j, k).
  • Write the result matrix of zeros before the loops.
Common mistakes
  • Swapping the indexes: B[j][k] instead of B[k][j].
  • Using = instead of += in the product loop, which keeps only the last term.
  • Forgetting the length check before a dot product.

Practice questions (graded-assignment style)

Q1Dot product

Numerical Answer Type (NAT).

What does the program print?

Python
x = [1, 2, 3]
y = [4, 5, 6]
d = 0
for i in range(len(x)):
    d += x[i] * y[i]
print(d)
Solution
Idea

Multiply matching items and add.

Python
x = [1, 2, 3]
y = [4, 5, 6]
d = 0
for i in range(len(x)):
    d += x[i] * y[i]
print(d)
Output
32
  • 1·4 + 2·5 + 3·6 = 4 + 10 + 18 = 32.
  • Answer: 32
Q2Matrix item

Multiple Choice Question (MCQ): choose the one correct option.

What does the program print?

Python
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(A[1][2])
1
2
2
4
3
5
4
6
Solution
Idea

A[1][2] is row 1, column 2.

Python
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(A[1][2])
Output
6
  • Row 1 is [4, 5, 6] and its item at index 2 is 6.
  • Answer: option 4.
Q3A product cell

Numerical Answer Type (NAT).

What is the value printed?

Python
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
B = [[1, 2, 1], [6, 2, 3], [4, 2, 1]]
print(sum(A[1][k] * B[k][0] for k in range(3)))
Solution
Idea

Row 1 of A against column 0 of B.

Python
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
B = [[1, 2, 1], [6, 2, 3], [4, 2, 1]]
print(sum(A[1][k] * B[k][0] for k in range(3)))
Output
58
  • 4·1 + 5·6 + 6·4 = 4 + 30 + 24 = 58.
  • Answer: 58
Q4Multiplications

Numerical Answer Type (NAT).

How many multiplications does the triple-loop program do for two 4 × 4 matrices?

Solution
Idea

n × n × n.

  • 4 × 4 × 4 = 64.
  • Answer: 64
Q5Sum of matrices

Multiple Choice Question (MCQ): choose the one correct option.

What does the program print?

Python
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
S = [[0, 0], [0, 0]]
for i in range(2):
    for j in range(2):
        S[i][j] = A[i][j] + B[i][j]
print(S)
1
[[6, 8], [10, 12]]
2
[[5, 12], [21, 32]]
3
[[1, 2], [3, 4]]
4
[[6, 8, 10, 12]]
Solution
Idea

Add the items in the same position.

Python
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
S = [[0, 0], [0, 0]]
for i in range(2):
    for j in range(2):
        S[i][j] = A[i][j] + B[i][j]
print(S)
Output
[[6, 8], [10, 12]]
  • 1+5, 2+6 in row 0 and 3+7, 4+8 in row 1.
  • Answer: option 1.
Q6Matrix facts

Multiple Select Question (MSQ): select all that apply.

Choose the correct statements.

1
A matrix can be stored as a list of lists
2
Matrix addition needs two nested loops
3
Matrix multiplication needs three nested loops
4
A dot product needs the lists to be of different lengths
Solution
Idea

A dot product needs equal lengths.

  • Statement 4 is false: the two lists must have the same length.
  • Answer: options 1, 2 and 3.
Revision overview
Dot productSum of x[i] * y[i]
Equal lengths first
1·8 + 7·6 + 3·3 + 4·2 = 67
MatrixList of row-lists
M[i][j]: row i, column j
Start the result with zeros
Add and multiplyAdd: 2 nested loops
Multiply: 3 nested loops
C[i][j] += A[i][k] * B[k][j]
Dot product and matricesChapter 6
Cost3 x 3 x 3 = 27 products
n cubed for n x n
Source noteC[2][1] is row index 2, column index 1
value 48
Shortcutnumpy: X * Y
(not run here)
Revision: dot product and matrices on one page
Next · PracticeTry It Yourself
07
Practice

Try It Yourself

The ten tasks of the source, each with a worked and executed solution.

Ten tasks from the source, to be done without looking back at the examples. Each has a worked, really executed solution. Try the task first, then compare.

7.1Tasks 1 to 5: creating, indexing, slicing, adding and deleting

Example 10Task 1: creating a list

Create a list of your 5 favourite movies and print it.

Solution
Python
movies = ["Inception", "Interstellar", "Tenet", "Dunkirk", "Oppenheimer"]
print(movies)
Output
['Inception', 'Interstellar', 'Tenet', 'Dunkirk', 'Oppenheimer']

Note that print shows the list with square brackets and single quotes, even though the code used double quotes.

Example 11Task 2: indexing

Given l = [10, 20, 30, 40, 50], print the 1st, 3rd, and last item using indexing.

Solution
Python
l = [10, 20, 30, 40, 50]
print(l[0], l[2], l[-1])
Output
10 30 50

The 1st item is index 0, the 3rd item is index 2, and the last is -1.

Example 12Task 3: slicing

From l = [1, 2, 3, 4, 5, 6, 7, 8], print the middle four items using a single slice.

Solution
Python
l = [1, 2, 3, 4, 5, 6, 7, 8]
print(l[2:6])
Output
[3, 4, 5, 6]
Example 13Task 4: append and insert

Start with l = [1, 2, 4, 5]. Insert 3 at the correct position so the list stays in order.

Solution
Python
l = [1, 2, 4, 5]
l.insert(2, 3)
print(l)
Output
[1, 2, 3, 4, 5]
Example 14Task 5: pop, remove, del

Given l = [5, 10, 15, 20, 25], remove 15 by value, then delete the last item using del.

Solution
Python
l = [5, 10, 15, 20, 25]
l.remove(15)
del l[-1]
print(l)
Output
[5, 10, 20]

After remove(15) the list is [5, 10, 20, 25]; del l[-1] then deletes 25.

7.2Tasks 6 to 10: in, extend, looping, sorting and the combined task

Example 15Task 6: the in operator

Ask the user for a name and check whether it exists in a list of registered students.

Solution
Python
students = ["Asha", "Ravi", "Meera"]
name = input("Enter a name: ")
print("Registered" if name in students else "Not registered")
Output
Enter a name: Ravi
Registered
Python
students = ["Asha", "Ravi", "Meera"]
name = input("Enter a name: ")
print("Registered" if name in students else "Not registered")
Output
Enter a name: Kiran
Not registered

The first run types Ravi (found), the second types Kiran (not found). The check is case-sensitive: "ravi" would not match "Ravi".

Example 16Task 7: extend

Merge two lists of even and odd numbers into one combined, sorted list.

Solution
Python
evens = [2, 4, 6]
odds = [1, 3, 5]
evens.extend(odds)
evens.sort()
print(evens)
Output
[1, 2, 3, 4, 5, 6]

extend adds the odd numbers to the end, then sort() puts the whole list in order.

Example 17Task 8: looping

Loop through a list of numbers and print only the ones greater than 10.

Solution
Python
nums = [3, 12, 7, 18, 9, 25]
for n in nums:
    if n > 10:
        print(n)
Output
12
18
25
Example 18Task 9: sorting

Write your own selection-sort program to sort a list of 8 numbers in descending order.

Solution
Python
l = [33, 7, 19, 2, 41, 15, 8, 27]
x = []
while len(l) > 0:
    maxVal = l[0]
    for i in range(len(l)):
        if l[i] > maxVal:
            maxVal = l[i]
    x.append(maxVal)
    l.remove(maxVal)
print(x)
Output
[41, 33, 27, 19, 15, 8, 7, 2]
Example 19Task 10: combined: remove duplicates

Write a program that removes duplicate numbers from a list of 20 random numbers between 1 and 10.

Solution

The source program has no seed, so its output changes every run. Here a random.seed(2) and an extra print of the original list are added so the result can be checked:

Python
import random
random.seed(2)
l = []
for i in range(20):
    l.append(random.randint(1, 10))
print(l)
unique = []
for n in l:
    if n not in unique:
        unique.append(n)
print(unique)
Output
[1, 2, 2, 6, 3, 5, 5, 10, 4, 10, 1, 10, 3, 7, 7, 9, 6, 9, 8, 9]
[1, 2, 6, 3, 5, 10, 4, 7, 9, 8]

The idea: scan the original list and append a number to unique only if it is not already there (not in). The result keeps the order of first appearance; it is not sorted.

Python
l = [3, 1, 3, 2, 1]
print(list(dict.fromkeys(l)))
print(sorted(set(l)))
Output
[3, 1, 2]
[1, 2, 3]

The two extra lines are shorter ways: dict.fromkeys keeps the order, set loses it (extra).

7.3Going deeper

  • Make your own checks. For each task write down the expected output before running. If the two differ, trace the code with a table until you find the line responsible.
  • Variations to try (extra). Task 3 with a list of any even length; Task 8 printing the count instead; Task 9 ascending using the min version; Task 10 keeping the duplicates only.

7.4Exam tips and common mistakes

Exam tips
  • Read the task for the output format: a list, one item per line, or a single value.
  • Test every solution on a small list you can check by hand.
Common mistakes
  • Using l.append(a, b): append takes exactly one item.
  • Comparing a number read with input() to numbers in a list without int(...).
  • Printing inside the loop when the task asks for the whole list at the end.

Practice questions (graded-assignment style)

Q1Task 5 check

Multiple Choice Question (MCQ): choose the one correct option.

What does the program print?

Python
l = [5, 10, 15, 20, 25]
l.remove(15)
del l[-1]
print(l)
1
[5, 10, 20]
2
[5, 10, 20, 25]
3
[10, 20, 25]
4
[5, 10, 15, 20]
Solution
Idea

Remove the value 15, then the last item.

Python
l = [5, 10, 15, 20, 25]
l.remove(15)
del l[-1]
print(l)
Output
[5, 10, 20]
  • remove(15) gives [5, 10, 20, 25]; del l[-1] removes 25.
  • Answer: option 1.
Q2Task 7 check

Multiple Choice Question (MCQ): choose the one correct option.

What does the program print?

Python
evens = [2, 4, 6]
odds = [1, 3, 5]
evens.extend(odds)
evens.sort()
print(evens)
1
[2, 4, 6, 1, 3, 5]
2
[1, 2, 3, 4, 5, 6]
3
[6, 5, 4, 3, 2, 1]
4
None
Solution
Idea

extend first, then sort.

Python
evens = [2, 4, 6]
odds = [1, 3, 5]
evens.extend(odds)
evens.sort()
print(evens)
Output
[1, 2, 3, 4, 5, 6]
  • After extend the list is [2, 4, 6, 1, 3, 5], and sort() orders it.
  • Answer: option 2.
Q3Unique count

Numerical Answer Type (NAT).

How many numbers does unique hold at the end?

Python
l = [4, 4, 9, 2, 9, 4]
unique = []
for n in l:
    if n not in unique:
        unique.append(n)
print(len(unique))
Solution
Idea

A number is added only if it is not already there.

Python
l = [4, 4, 9, 2, 9, 4]
unique = []
for n in l:
    if n not in unique:
        unique.append(n)
print(len(unique))
Output
3
  • The distinct numbers in [4, 4, 9, 2, 9, 4] are 4, 9, 2: three.
  • Answer: 3
Q4The in operator

Multiple Choice Question (MCQ): choose the one correct option.

What does the program print?

Python
students = ["Asha", "Ravi", "Meera"]
name = "ravi"
print("Registered" if name in students else "Not registered")
1
Registered
2
Not registered
3
An error
4
Nothing
Solution
Idea

The search is case-sensitive.

Python
students = ["Asha", "Ravi", "Meera"]
name = "ravi"
print("Registered" if name in students else "Not registered")
Output
Not registered
  • "ravi" is not equal to "Ravi", so it is not in the list.
  • Answer: option 2.
Q5Task facts

Multiple Select Question (MSQ): select all that apply.

Choose the correct statements.

1
l.sort() returns the sorted list
2
extend followed by sort merges and orders two lists
3
The duplicate-removing loop keeps the order of first appearance
4
A middle slice of 8 items is l[2:6]
Solution
Idea

sort changes the list in place.

  • Statement 1 is false: sort() returns None.
  • Answer: options 2, 3 and 4.
Next · SummaryWeek 4 Summary Sheet
08
Summary

Week 4 Summary Sheet

One page to revise all of Week 4: methods, programs, the checklist and the common errors.

Everything from Week 4 on one page: the method table, how the methods differ, the programs of the week, the checklist, and the usual mistakes.

8.1Revision sheet

TopicKey point
[ ]Creates a list, ordered and mutable
Indexingl[0] = first item
Negative indexingl[-1] = last item
Slicing l[a:b]a = start, b = end (not included)
append(x)Adds x to the end
insert(i, x)Inserts x at index i
pop()Removes the last item
remove(x)Removes the first item equal to x
del l[i]Deletes the item at index i
len(l)Number of items in the list
l[i] = xChanges the value at index i
x in lTrue / False if x is present
extend(l2)Appends all items of l2 to l
for i in l:Loops through every item
Matrix as a listA list of row-lists; M[i][j] = row i, col j
Matrix multiplyNeeds 3 nested loops (i, j, k)

8.2Which method, and what it returns

StatementChanges l?Returns
l.append(x)yesNone
l.insert(i, x)yesNone
l.extend(l2)yesNone
l.pop()yesthe removed item
l.remove(x)yesNone
del l[i]yes(a statement, not a function)
l.sort()yesNone
sorted(l)noa new sorted list
l[a:b]noa new list
len(l), x in lnoa number, True/False

8.3The programs of the week

ProgramIdeaResult
Birthday paradoxsort, then compare neighbours30 people: about 70.6% chance of a repeat
Naive (linear) searchscan from the front, break on a matchup to 100,000 comparisons
Selection sortfind the smallest, move it, remove it[5, 6, 7, 7, 8, 10, 12, 18, 35, 42]
Dot productsum of x[i] * y[i]67
Matrix additionC[i][j] = A[i][j] + B[i][j][[2, 4, 4], [10, 7, 9], [11, 10, 10]]
Matrix multiplicationC[i][j] += A[i][k] * B[k][j][[25, 12, 10], [58, 30, 25], [91, 48, 40]]

8.4Last-minute checklist

  • Creating a list and mixing data types in one
  • Positive and negative indexing
  • Slicing with [start:end], and omitting either side
  • append() vs insert()
  • pop() vs remove() vs del
  • len() on a list
  • Changing an item by index assignment
  • The in operator for membership checks
  • extend() vs append() with a list argument
  • Looping through a list with for
  • Selection-sort logic (find min/max, move, remove)
  • Linear search logic
  • Representing a matrix as a list of lists
  • Matrix addition vs matrix multiplication (2 vs 3 nested loops)

8.5Common errors to avoid

MistakeCorrect idea
l[len(l)]Out of range. The last item is l[len(l) - 1] or l[-1].
l = l.append(x)append returns None: use l.append(x) alone.
l1.append(l2)Adds the list as one item. Use extend to add its items.
b = aNot a copy: both names share one list. Use a[:].
removing inside a for loopSkips items. Build a new list instead.
l.sort() printedPrints None. Use sorted(l) or sort first.
l[a:b] with b includedThe end index is not included.
Revision overview
Basics[ ] ordered, mutable
Index 0.. and -1..
Slices l[a:b]
Changeappend, insert, extend
pop, remove, del
l[i] = x
Looklen(l), x in l
for x in l
range(len(l))
Week 4Lists
ProgramsBirthday paradox
Linear search
Selection sort
MatricesList of lists
Dot product
2 vs 3 nested loops
AvoidIndex out of range
append vs extend
b = a is not a copy
Revision: all of Week 4 on one page
Back to top