All 2.3 22 min read 10 question quiz 17 exam marks

Lists

Storing many values in order, indexing and slicing them, the methods that change them, and the list comprehension that replaces four lines with one.

Lists

Python
scores = [45, 78, 12, 90, 33]
names = ["Aisha", "Ben", "Chloe"]
mixed = [1, "two", 3.0, True]
empty = []

Accessing items

Python
print(scores[0])      # 45, the first
print(scores[-1])     # 33, the last
print(scores[1:4])    # [78, 12, 90], a slice
print(len(scores))    # 5
Indexes start at 0

A list of 5 items has indexes 0 to 4. Negative indexes count backwards, so -1 is the last item.

Changing a list

Python
scores[0] = 50            # replace an item
scores.append(60)         # add to the end
scores.insert(1, 99)      # insert at a position
scores.remove(12)         # remove the first occurrence of the value 12
popped = scores.pop()     # remove and return the last item
del scores[0]             # delete by index
scores.sort()             # sort in place
scores.reverse()          # reverse in place
scores.clear()            # empty it

Useful functions

Python
print(sum(scores), max(scores), min(scores), len(scores))
print(sorted(scores))              # a sorted copy, original untouched
print(scores.count(45))            # how many times 45 appears
print(scores.index(78))            # position of the first 78
print(78 in scores)                # True or False

List comprehensions

A compact way to build a list from another list.

Python
squares = [n * n for n in range(1, 6)]           # [1, 4, 9, 16, 25]
evens = [n for n in scores if n % 2 == 0]        # only the even scores
upper = [name.upper() for name in names]         # every name in capitals

The long form of the first one is:

Python
squares = []
for n in range(1, 6):
    squares.append(n * n)

Both are correct. Use whichever is clearer for the situation.

A list is a row of numbered lockersA list drawn as a row of numbered lockers, with the code that changes it. The list scores starts with five values, 72, 85, 90, 64 and 78, in lockers numbered from 0. The call append of 88 adds a new locker on the end, so the list now has six items. The assignment scores, bracket, 1, bracket, equals 99 replaces what was in locker 1, the 85, with 99. Reading scores bracket 2 opens locker 2 and finds 90, and len of scores is 6, so the last index is 5, one less than the length. The program prints 90 and 6.lists.py1scores = [72, 85, 90, 64, 78]2scores.append(88)3scores[1] = 994print(scores[2], len(scores))output90 6the item at index 2, then the length720991902643784885appendchangedscores[2]len(scores) is 6, so the last index is 51234
  1. The list holds its items in order, and the first locker is number 0.
  2. append() puts a new item on the end, so the list grows.
  3. Assign to an index to replace what is there. The old value is gone.
  4. scores[2] opens locker 2. len(scores) counts the lockers, so the last index is one less.
Index from 0, and remember the last index is len minus 1.
Five list methods, each shown on the list 45, 78, 12, 90, 33 before and afterFive list methods, each starting from the list 45, 78, 12, 90, 33. append of 60 adds 60 on the end. insert of 1 comma 99 puts 99 at index 1 and the rest move along. remove of 12 takes out the first 12 and the rest close up, leaving 45, 78, 90, 33. pop takes off the last item, 33, and returns it, leaving 45, 78, 12, 90. sort puts the list in place in order, smallest first: 12, 33, 45, 78, 90.scores = [45, 78, 12, 90, 33]each row starts from this list1scores.append(60)4578129033457812903360added to the end2scores.insert(1, 99)4578129033459978129033inserted at index 1, the rest move along3scores.remove(12)457812903345789033the first 12 is removed, the rest close up4scores.pop()457812903345781290the last item is removed and returned: 335scores.sort()45781290331233457890sorted in place, smallest first
  1. append() adds to the end.
  2. insert() puts an item at a position, and the rest move along.
  3. remove() takes out the first item with that value.
  4. pop() removes the last item and gives it back.
  5. sort() orders the list in place.
These methods change the list itself. They do not make a copy.

Lists in practice

Reading a list of names and sorting them

Python
names = []

while True:
    entry = input("Enter a name, or press enter to finish: ").strip()
    if entry == "":
        break
    names.append(entry.title())

names.sort()
print(f"\n{len(names)} names, in order:")
for i, name in enumerate(names, start=1):
    print(f"{i}. {name}")

Basic statistics

Python
marks = [67, 82, 45, 91, 58, 73]

marks_sorted = sorted(marks)
middle = len(marks_sorted) // 2

if len(marks_sorted) % 2 == 1:
    median = marks_sorted[middle]
else:
    median = (marks_sorted[middle - 1] + marks_sorted[middle]) / 2

print(f"Mean:   {sum(marks) / len(marks):.1f}")
print(f"Median: {median}")
print(f"Range:  {max(marks) - min(marks)}")

Key terms you must be able to define

List
An ordered collection of values accessed by index, which can be changed.
Index
The position of an item in a list or string, counting from 0.
Slice
A section of a list or string, taken with a start and end index.
append
A list method that adds an item to the end.
List comprehension
A compact expression that builds a new list from an existing one.
Mutable
Can be changed after creation. Lists are mutable, which is why append works.
sort
A list method that reorders the list itself and returns nothing.
sorted
A built in function that returns a new sorted list, leaving the original alone.
enumerate
A built in function giving both the index and the item while looping.

Flashcards

Say the definition out loud before you flip the card. Retrieving it from memory is what builds recall, reading it again is not.

DefineList
AnswerAn ordered collection of values accessed by index, which can be changed.
DefineIndex
AnswerThe position of an item in a list or string, counting from 0.
DefineSlice
AnswerA section of a list or string, taken with a start and end index.
Defineappend
AnswerA list method that adds an item to the end.
DefineList comprehension
AnswerA compact expression that builds a new list from an existing one.
DefineMutable
AnswerCan be changed after creation. Lists are mutable, which is why append works.
Definesort
AnswerA list method that reorders the list itself and returns nothing.
Definesorted
AnswerA built in function that returns a new sorted list, leaving the original alone.

Reach the top grade on this topic

  • Use negative indexes and slices confidently, including [::-1] to reverse
  • Know the difference between sort() and sorted(), and choose deliberately
  • Use append, insert, remove and del knowing exactly which works by value and which by index
  • Know when a list comprehension is clearer than a loop, and when it is not
  • Use enumerate rather than a counter variable you maintain by hand

Mistakes that cost marks here

Avoid these
  • Using an index equal to the length of the list, which is out of range.
  • Using remove() with an index. It removes by value, del removes by index.
  • Forgetting that sort() changes the list while sorted() returns a copy.
  • Expecting list2 = list1 to make a copy. Both names refer to the same list.
  • Removing items from a list while looping over it, which silently skips entries.
Fun fact

A Python list can hold anything at all, including other lists, and the items do not have to be the same type. That flexibility is why a list is the first structure you reach for, and why a dictionary is sometimes the better one.

Knowledge check

0 of 10 answered
1What does scores[-1] give?
2What does scores[1:3] give for [45, 78, 12, 90]?
3What does len([4, 8, 15]) give?
4How do you add an item to the end of a list?
5What does scores.append(5) do?
6What is the difference between sort() and sorted()?
7What does numbers[::-1] give?
8What is the difference between remove(3) and del numbers[3]?
9After list2 = list1, what does changing list2 do?
10What does [n * 2 for n in [1, 2, 3]] produce?
Score 0 / 10

Exam-style questions

Five written questions in the style of the real paper. Write a full answer, then let the marker check it against the mark scheme.

17 marks
1Explain the difference between the list methods sort() and the function sorted().3 marks
Explain

Mark scheme0 / 3
  • Lsort() rearranges the original list in place
  • Lsorted() returns a new sorted list
  • LUse sorted() when the original order must be kept
Model answer

The sort method rearranges the items of the list it is called on, changing the original list permanently and returning nothing. The sorted function leaves the original list completely untouched and returns a brand new list containing the same items in order. Which one to use depends on whether the original order still matters: if it does, sorted must be used, otherwise the original ordering is lost and cannot be recovered.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
2Write a program that stores five marks in a list and prints the highest, the lowest and the mean.5 marks
Write

Mark scheme0 / 5
  • LCreates a list containing the five marks
  • LUses max to find the highest
  • LUses min to find the lowest
  • LUses sum divided by len for the mean
  • LPrints all three results
Model answer

marks = [67, 82, 45, 91, 58] print(f"Highest: {max(marks)}") print(f"Lowest: {min(marks)}") print(f"Mean: {sum(marks) / len(marks):.1f}") The built in functions max, min, sum and len all take the list directly, so no loop is needed at all. The mean is the total divided by how many items there are, and len is used rather than typing 5 so that the program still works correctly if marks are added or removed later.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
3Explain the difference between numbers.remove(3) and del numbers[3].3 marks
Explain

Mark scheme0 / 3
  • Lremove searches for the value 3 and deletes the first match
  • Ldel deletes whatever item is at index 3
  • LThey affect different items unless the value happens to equal the index
Model answer

The remove method takes a value rather than a position. It searches the list from the start and deletes the first item equal to that value, raising a ValueError if no item matches. The del statement takes a position and removes whatever item currently sits at that index, raising an IndexError if the index does not exist. For the list [10, 20, 3, 40], remove(3) deletes the value 3 while del numbers[3] deletes 40, so confusing the two silently removes the wrong item.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
4A programmer writes copy = original and then appends to copy. Explain why original changes as well.3 marks
Explain

Mark scheme0 / 3
  • LAssignment copies the reference, not the list itself
  • LBoth names therefore refer to one single list in memory
  • LUse copy() or list() to make a genuine copy
Model answer

Assigning one list to another name does not duplicate the list. It makes the new name refer to exactly the same list already held in memory, so there is only ever one list with two labels attached to it. Appending through either name therefore changes what is seen through both. To create a genuinely separate list the programmer must copy the contents, using original.copy(), list(original) or the slice original[:], each of which builds a new list containing the same items.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
5A program uses numbers[5] on a list containing five items and crashes. Explain why and state the valid range of indexes.3 marks
Explain

Mark scheme0 / 3
  • LList indexes start at 0
  • LFive items therefore occupy indexes 0 to 4
  • LIndex 5 is out of range so an IndexError occurs
Model answer

Python lists are indexed from 0, so the first item is at index 0 rather than index 1. A list containing five items therefore occupies indexes 0, 1, 2, 3 and 4, and there is no item at index 5. Attempting to access it raises an IndexError and the program stops. The highest valid index is always the length of the list minus one, which is why loops over a list use range(len(numbers)) rather than range(1, len(numbers) + 1).

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
Machine mark 0 / 17