S2Q1 · Get Words That Come After "the"¶
⚡ Quick Reference
Function: get_words_after_the(sentence: str) -> list
Core idea: split the sentence into words, iterate with index, collect the word immediately after any word that equals "the" (case-insensitive).
def get_words_after_the(sentence: str) -> list:
words = sentence.split()
return [words[i + 1] for i in range(len(words) - 1)
if words[i].lower() == "the"]
Key rules:
- Case-insensitive: word.lower() == "the"
- Collect the word at i + 1 whenever words[i] matches
- Stop at len(words) - 1 - the last word can't be followed by another
- Preserve the original case of the word that follows
Problem Statement¶
Problem
Write a function get_words_after_the(sentence) that returns a list of all words that immediately follow "the" (case-insensitive) in the sentence.
Examples:
"The key and the lock is there"
['key', 'lock']
"The the and the The"
['the', 'and', 'The']
Tracing both examples¶
Example 1: "The key and the lock is there"
i |
words[i] |
lower() |
Matches "the"? | words[i+1] |
|---|---|---|---|---|
| 0 | "The" |
"the" |
✅ | "key" ← collect |
| 1 | "key" |
"key" |
❌ | - |
| 2 | "and" |
"and" |
❌ | - |
| 3 | "the" |
"the" |
✅ | "lock" ← collect |
| 4 | "lock" |
"lock" |
❌ | - |
| 5 | "is" |
"is" |
❌ | - |
| 6 | "there" |
"there" |
❌ | - (last word, skip) |
Result: ["key", "lock"] ✓
Example 2: "The the and the The"
i |
words[i] |
Matches? | words[i+1] |
|---|---|---|---|
| 0 | "The" |
✅ | "the" ← collect |
| 1 | "the" |
✅ | "and" ← collect |
| 2 | "and" |
❌ | - |
| 3 | "the" |
✅ | "The" ← collect |
| 4 | "The" |
- | (last word, skip) |
Result: ["the", "and", "The"] ✓
Solution approaches¶
Key takeaways¶
range(len(words) - 1) avoids index out of bounds
Checking words[i+1] requires i+1 to be a valid index. Stopping at len(words) - 1 ensures the last word (which has no successor) is never treated as a potential "the".
zip(words, words[1:]) - clean adjacent pair pattern
Pairing a list with its own slice shifted by one gives all consecutive pairs without explicit indexing. zip(a, b) stops at the shorter iterable - naturally skipping the last element.
Preserve original case of the following word
The search is case-insensitive (use .lower() on the potential "the"), but the collected word must keep its original casing. Never apply .lower() to words[i+1].