easy +10 pts

Group names by first letter

Organize a list of names into a dictionary keyed by each name's first letter.

Write a function `group_by_first_letter(names)` that takes a list of strings (names) and returns a dictionary where each key is a lowercase first letter (character) of a name, and the value is a list of all names (original case) that start with that letter, preserving the order they appear in the input. - If the input list is empty, return an empty dictionary. - Assume all names are non-empty strings. - The keys in the result should be sorted alphabetically (standard Python dictionary order is fine). Example: `group_by_first_letter(["Alice", "bob", "andy", "Bob", "carol"])` should return `{'a': ['Alice', 'andy'], 'b': ['bob', 'Bob'], 'c': ['carol']}`.

Constraints

0 <= len(names) <= 1000. Each name is a non-empty string of letters (at least one character).

Example

>>> group_by_first_letter(["Alice", "bob", "andy", "Bob", "carol"])
{'a': ['Alice', 'andy'], 'b': ['bob', 'Bob'], 'c': ['carol']}
>>> group_by_first_letter(["Zoe", "amy", "zack"])
{'a': ['amy'], 'z': ['Zoe', 'zack']}
>>> group_by_first_letter([])
{}
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use a dictionary to accumulate lists for each first letter.
Access the first character with `name[0]` and convert to lowercase using `.lower()`.
To preserve original order, append each name to the list for its key as you iterate.
No need to sort keys explicitly; Python keeps insertion order and keys will appear in first occurrence order.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.