S11 · Computer Studies I · Grade 10 · ages 15–16
Lists & Searching
Data structures, sorting and searching
Grade 10 · 3.5 Data StructuresCBE strandFree · works offline
▶ Start the interactive lessonThe full lesson adds narration, an interactive mission and a 10-question bank, and the quiz explains any mistakes.
Lists
A list holds many values in order, in square brackets: marks = [70, 85, 90]. Positions (indexes) start at 0, so marks[0] is 70 and marks[2] is 90. Lists can change: marks.append(65) adds a value at the end, and len(marks) tells you how many items there are.
Tuples and sets
A tuple is like a list but cannot be changed once made, in round brackets: point = (3, 4). Use one for data that should stay fixed. A set holds unique values in curly brackets: {"Maths", "English"}. Adding a duplicate does nothing, which makes sets great for removing repeats.
Dictionaries
A dictionary stores key and value pairs, like a phone book: phones = {"Amani": "0712 000 000"}. You look values up by key, phones["Amani"], instead of by position. Keys must be unique.
Two-dimensional lists
A list can hold other lists, like a table with rows and columns: table = [[1, 2], [3, 4]]. table[1][0] means row 1, column 0, which is 3. This is useful for timetables, spreadsheets and game boards.
Searching and sorting
Linear search checks items one by one from the start; it works on any list but is slow for big ones. Binary search needs a sorted list: check the middle, then throw away the half that can't contain the target, and repeat, which is very fast. Bubble sort sorts by repeatedly swapping neighbours that are in the wrong order.
Key points
- A list holds values in order, and its positions start at zero.
- append adds to a list; len counts its items.
- A tuple can't be changed once made.
- A set keeps only unique values.
- A dictionary stores key and value pairs, like a phone book.
- Linear search checks every item; binary search halves a sorted list each time.
- Bubble sort swaps neighbours until everything is in order.
Try it for real: Class marks
- If you can, open Python and make a list of five marks. Print the highest with max() and the average with sum() divided by len().
- Make a dictionary of three classmates and their favourite subjects, then print one of them by name.