Problem No 11
Group Words by First Letter with a Dictionary
Medium≈ 15 minute session
Lesson guide
What this Python exercise practices
Group Words by First Letter with a Dictionary is a intermediate practice lesson that focuses on lists, iteration, filtering. It is designed to be solved in about 15 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- List values
- Basic indexing
Difficulty and time
- Level
- Intermediate
- Estimated time
- 15 minutes
Practice path
Summary
Organize a list of words into groups keyed by their first letter (case-insensitive), preserving original order within groups.
Problem statement
Given a list of non-empty strings, group the words by their first letter in a case-insensitive way. The grouping key should be the lowercase version of the first character. Preserve the order of words as they appear in the input for each group's list. Return a dictionary where keys are single-character strings and values are lists of original words.
Task
Implement a function that groups non-empty words by their lowercase first character and returns a dictionary mapping each letter to the list of words that start with it, preserving input order.
Examples
Simple grouping
Input
group_by_first_letter(['apple', 'ant', 'banana'])
Output
{'a': ['apple', 'ant'], 'b': ['banana']}
Explanation
Words starting with 'a' are grouped under key 'a', and 'banana' under 'b'. Order within groups matches input order.
Input format
A list of non-empty strings.
Output format
A dictionary mapping each lowercase first letter to a list of words (strings) that start with that letter.
Constraints
Words are non-empty strings. The function should be case-insensitive for grouping (use lowercase for keys). Preserve original word order in each group's list.
Samples
Sample input 0
['Dog', 'deer', 'Cat']
Sample output 0
{'d': ['Dog', 'deer'], 'c': ['Cat']}
Explanation 0
Grouping is case-insensitive; 'Dog' and 'deer' group under 'd'.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.