Program 9
Program 9
TITLE : Program to create a dictionary with key as first character and value as words starting
with that character.
Problem Description : The program takes a string and creates a dictionary with key as first
character and value as words starting with that character.
Theory / Analysis
Algorithm
4. Using a for loop and if statement check if the word already present as a key in the dictionary.
5. If it is not present, initialize the letter of the word as the key and the word as the value and
append it to a sublist created in the list.
8. Exit.
Program
Python Program to create a dictionary with key as first character and value as words starting with
that character. The program output is also shown below.
test_string=raw_input("Enter string:")
l=test_string.split()
d={}
for word in l:
d[word[0]]=[]
d[word[0]].append(word)
else:
d[word[0]].append(word)
print(k,":",v)
Runtime Output
Case 1:
Case 2: