Skip to content

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:

Input
"The key and the lock is there"
Output
['key', 'lock']
Input
"The the and the The"
Output
['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

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"]
def get_words_after_the(sentence: str) -> list:
    words  = sentence.split()
    result = []
    for i in range(len(words) - 1):   # stop before last word
        if words[i].lower() == "the":
            result.append(words[i + 1])
    return result
def get_words_after_the(sentence: str) -> list:
    words = sentence.split()
    return [b for a, b in zip(words, words[1:])
            if a.lower() == "the"]

zip(words, words[1:]) creates pairs of adjacent words (words[i], words[i+1]). Filter on the first element - elegant and no index arithmetic needed.

def get_words_after_the(sentence: str) -> list:
    words = sentence.split()
    pairs = zip(words, words[1:])
    return list(map(lambda p: p[1],
                    filter(lambda p: p[0].lower() == "the", pairs)))

Key takeaways

01

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".

02

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.

03

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].