Group by First Letter
Completed
Read a line of words. Group them by their first letter and print each group as letter: word word, letters in alphabetical order, words in input order.
Input
One line.
Output
One line per letter.
Example
Input
apple bat avocado cat banana
Output
a: apple avocado b: bat banana c: cat
Editor
Python
Hints
setdefault(key, []) returns the list for key, creating it first if needed.
Solution explanation
Grouping into lists inside a dict is a very common pattern. collections.defaultdict(list) is another way.
Show solution
groups = {}
for w in input().split():
groups.setdefault(w[0], []).append(w)
for letter in sorted(groups):
print(f"{letter}: {' '.join(groups[letter])}")