I have one question about writing a dictionary given a file. I have done the coding but I am really confused. The parameter name is “load_words: (file)” and the output expected is “dict of {str: list of strs}“.
The description of the problem given is:
“The open file contains one lowercase word per line. Return a dictionary in which each key is a single lowercase letter and each value is a list of the words from the file that start with that letter. Only letters that one or more words from the file start with appear as keys in the dictionary. “
I am trying to write my code for
def load_words(file1):
I really don’t know how to approach this question any help would be appreciated any hints or even a complete solution and I could work backwards.
NOTE: IS NOT A HOMEWORK PROBLEM. I have a midterm in 2 days and I am trying to do the past midterms so please help
The simplest way to do this is to use
collections.defaultdictas follows:However, for your midterm, your professor might be expecting this answer:
Hope that helps