0% found this document useful (0 votes)
1 views

Program 9

The document describes a program that creates a dictionary where the keys are the first characters of words from a given string, and the values are lists of words starting with those characters. It includes an algorithm outlining the steps to implement the program and provides a Python code example along with runtime output for two test cases. The program effectively categorizes words based on their initial letters.

Uploaded by

newshunt535
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Program 9

The document describes a program that creates a dictionary where the keys are the first characters of words from a given string, and the values are lists of words starting with those characters. It includes an algorithm outlining the steps to implement the program and provides a Python code example along with runtime output for two test cases. The program effectively categorizes words based on their initial letters.

Uploaded by

newshunt535
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Practical No.

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

1. Enter a string and store it in a variable.

2. Declare an empty dictionary.

3. Split the string into words and store it in a list.

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.

6. If it is present, add the word as the value to the corresponding sublist.

7. Print the final dictionary.

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:

if(word[0] not in d.keys()):

d[word[0]]=[]
d[word[0]].append(word)

else:

if(word not in d[word[0]]):

d[word[0]].append(word)

for k,v in d.items():

print(k,":",v)

Runtime Output

Case 1:

Enter string:Hello world this is a test string sanfoundry

('a', ':', ['a'])

('i', ':', ['is'])

('H', ':', ['Hello'])

('s', ':', ['sanfoundry', 'string'])

('t', ':', ['test', 'this'])

('w', ':', ['world'])

Case 2:

Enter string:python is my most favourite programming language in the entire world

('e', ':', ['entire'])

('f', ':', ['favourite'])

('i', ':', ['in', 'is'])

('m', ':', ['most', 'my'])

('l', ':', ['language'])

('p', ':', ['programming', 'python'])

('t', ':', ['the'])

('w', ':', ['world'])

You might also like