Question

write a function firstLetterWords(words) that takes as a parameter a list of strings named words and...

write a function

firstLetterWords(words)

that takes as a parameter a list of strings named words and returns a dictionary with lower case letters as keys. But now associate with each key the list of the words in words that begin with that letter. For example, if the list is ['ant', 'bee', 'armadillo', 'dog', 'cat'], then your function should return the dictionary {'a': ['ant', 'armadillo'], 'b': ['bee'], 'c': ['cat'], 'd': ['dog']}.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def firstLetterWords(words):
    d = dict()
    for x in words:
        if not d.get(x[0]):
            d[x[0]] = [x]
        else:
            lst = d[x[0]]
            lst.append(x)
            d[x[0]] = lst
    return d

print(firstLetterWords(['ant', 'bee', 'armadillo', 'dog', 'cat']))

Output:

{'a': ['ant', 'armadillo'], 'b': ['bee'], 'd': ['dog'], 'c': ['cat']}

Please up vote the solution if it helped. Thanks!

Add a comment
Know the answer?
Add Answer to:
write a function firstLetterWords(words) that takes as a parameter a list of strings named words and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT