Accept a sequence of words as input. Create a dictionary named real_dict whose keys are the letters of the English alphabet. For each key (letter), the corresponding value should be a list of words that begin with this key (letter). For any given key, the words should be appended to the corresponding list in the order in which they appear in the sequence. You can assume that all words of the sequence will be in lower case.You do not have to print the output to the console.
Question
Accept a sequence of words as input. Create a dictionary named real_dict whose keys are the letters of the English alphabet. For each key (letter), the corresponding value should be a list of words that begin with this key (letter). For any given key, the words should be appended to the corresponding list in the order in which they appear in the sequence. You can assume that all words of the sequence will be in lower case.You do not have to print the output to the console.
Solution
Here is a step-by-step Python solution for the problem:
-
Initialize an empty dictionary named
real_dict. In Python, you can do this by writingreal_dict = {}. -
Accept a sequence of words as input. You can do this using the
input()function in Python, which reads a line of text from the user. Since we want a sequence of words, we can split the input line into words using thesplit()function. Here is how you can do it:words = input().split(). -
Now, we need to iterate over each word in the sequence. In Python, you can do this using a
forloop. Here is how you can do it:for word in words:. -
For each word, we need to get the first letter. In Python, you can get the first character of a string
sby writings[0]. -
We need to check if this letter is already a key in
real_dict. In Python, you can check if a keykis in a dictionarydby writingk in d. -
If the letter is not a key in
real_dict, we need to add it and associate it with a list that contains the current word. In Python, you can add a key-value pair to a dictionarydby writingd[k] = v. -
If the letter is already a key in
real_dict, we need to append the current word to the list associated with this key. In Python, you can append an elementeto a listlby writingl.append(e).
Here is the complete Python code that implements these steps:
real_dict = {}
words = input().split()
for word in words:
first_letter = word[0]
if first_letter not in real_dict:
real_dict[first_letter] = [word]
else:
real_dict[first_letter].append(word)
This code does not print the output to the console, as specified in the problem statement.
Similar Questions
Write a Python program to create a new dictionary by extracting the mentioned keys and create a new new dictionary and print it as the result.
A dictionary contains words that are arranged in an … order.1 poinA. alphabetB. alphabetsC. alphabeticalD. alphabetically
Write a Python program to create a new dictionary by extracting the mentioned keys and create a new new dictionary and print it as the result.Constraints:Input Format:Output Format: Example:Input:{"name": "Kelly","age": 25,"salary": 8000,"city": "New york"}["name", "salary"]Output:{'name': 'Kelly', 'salary': 8000}Explanation:Dictionary = {"name": "Kelly","age": 25,"salary": 8000,"city": "New york"}Keys that needs to be extracted = ["name", "salary"]Result = {'name': 'Kelly', 'salary': 8000}Public Test Cases:# INPUT EXPECTED OUTPUT1 {"name": "Kelly","age": 25,"salary": 8000,"city": "New york"} ["name", "salary"] {'name': 'Kelly', 'salary': 8000}
Define a function first_word(arg1),with a string argument arg1. In the function:a. use split() function to separate arg1 by "," into a list of wordsb. Return res that is the last element of the list of words
How do you initialize a dictionary?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.