The dragon explains
Sorting, and the edge cases every pattern needs
The dragon: You already know .sort() sorts a list — one line, done. This is about HOW a computer could sort one, if it had to figure it out step by step.
The dragon: Selection sort: find the SMALLEST remaining item, swap it to the front of what's left unsorted, repeat.
example.pyPython 3values = [50, 20, 80, 10]# pass 0: smallest overall is 10, swap into position 0# [10, 20, 80, 50]# pass 1: smallest of [20, 80, 50] is 20, already in position 1# [10, 20, 80, 50]# pass 2: smallest of [80, 50] is 50, swap into position 2# [10, 20, 50, 80]# pass 3: only 80 left — nothing to compare, stays put# [10, 20, 50, 80]
The dragon: Each pass narrows the unsorted part by one item — the front keeps growing correctly sorted, one item at a time.
The dragon: This is a TEACHING tool, not something you'd write in real code — .sort() is always the right practical choice. The value is seeing that sorting isn't magic; it's a pattern like any other.
The dragon: Now, edge cases. Every pattern from this topic has an assumption baked in that can break.
The dragon: Max-tracking assumes there's at least ONE item — scores[0] crashes outright if scores is empty.
example.pyPython 3scores = []highest = scores[0]
OutputIndexError: list index out of rangeThe dragon: The counter pattern has a quieter trap: if every item is EXACTLY equal to what you're comparing against, a strict > test can reject all of them — 5 > 5 is False, even though they're not smaller.
example.pyPython 3scores = [5, 5, 5]count = 0for s in scores: if s > 5: count = count + 1print(count)
Output0The dragon: Neither of these is a bug in the PATTERN — they're the pattern working exactly as written, on an input its author didn't think to check for.
Quick check
Check your understanding
Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.
Question 1 of 2
scores = []. highest = scores[0] runs before any loop. What happens?
question.pyPython 3scores = []highest = scores[0]
- IndexError — position 0 doesn't exist in an empty listCorrect
- highest becomes 0, since that's a sensible default for an empty list
Why: IndexError. scores[0] asks for the FIRST item, and an empty list has no items at any position — not position 0, not any position. Any pattern that starts by grabbing scores[0] needs an empty-list check before it, or it crashes the instant the list happens to be empty.
Question 2 of 2
scores = [5, 5, 5], and the counter pattern counts values where s > 5. What's the final count?
question.pyPython 3count = 0for s in scores: if s > 5: count = count + 1
- 0 — 5 is never STRICTLY greater than 5Correct
- 3 — every item is 5, so every item should count
Why: 0. s > 5 is strictly greater-than, and 5 is never strictly greater than 5 — so every item fails the test, even though intuitively they all 'match' 5. Whether this is the right test depends entirely on what the problem actually asks for — > and >= genuinely disagree on this exact input.
In the game
What you'll practice
Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.
- 1
GuidedYour first try
values = [50, 20, 80, 10]. Selection sort repeatedly finds the SMALLEST remaining item and swaps it into place. Run the code as it is and watch values change after each pass — the final result should be [10, 20, 50, 80].
- +2
PracticeTwo more, on your own
This lesson is part of PRO.
PRO is a one-time $49.99 purchase, no subscription, and unlocks this whole topic along with everything else. Topics 1–3 are free, start to finish.
Unlock this lesson with PROStuck three times? The game offers the answer — unasked.