| title | Group Words by First Letter |
|---|
Write a function that takes a list of words and groups them by their first letter into a dictionary.
Example
Example Input:
['apple', 'banana', 'apricot', 'blueberry', 'cherry']
Example Output:
{'a': ['apple', 'apricot'], 'b': ['banana', 'blueberry'], 'c': ['cherry']}
<template>
from collections import defaultdict
def group_by_first_letter(words):
<sol>
grouped = {}
for word in words:
first_letter = word[0].lower()
if first_letter not in grouped:
grouped[first_letter] = []
grouped[first_letter].append(word)
return grouped
</sol>
</template>
<suffix>
#Driver code
l=eval(input())
print(group_by_first_letter(l))
</suffix>['apple', 'banana', 'apricot', 'blueberry', 'cherry']
{'a': ['apple', 'apricot'], 'b': ['banana', 'blueberry'], 'c': ['cherry']}
['dog', 'deer', 'cat', 'camel']
{'d': ['dog', 'deer'], 'c': ['cat', 'camel']}
['kiwi', 'kangaroo', 'kiwi', 'kiwi']
{'k': ['kiwi', 'kangaroo', 'kiwi', 'kiwi']}
[]
{}