Python - Create a Dictionary with Key as First Character and Value as Words Starting with that Character - GeeksforGeeks

26 Nov, 2020
In this article, we will code a python program to create a dictionary with the key as the first character and value as words starting with that character.
Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only a single value as an element, Dictionary holds the key: value pair. Key-value is provided in the dictionary to make it more optimized.
Examples:
Input: Hello World
Output: {'H': ['Hello'], 
         'W': ['World']}

Input: Welcome to GeeksForGeeks
Output: {'W': ['Welcome'], 
         't': ['to'], 
         'G': ['GeeksForGeeks']}


We will save the string in a variable and declare an empty dictionary.
Then we will split the string into words and form a list of those words.
For each word, we will check if the key for that word is present or not.
If it is not then we will add that key and word to the dictionary and if it is already present then we will append that word to that key’s sublist.
Below is the implementation of the above approach:
Python3
# Python program to Create a Dictionary with Key as First# Character and Value as Words Starting with that Character  # Driver Code  # Declaring String Datastring_input = '''GeeksforGeeks is a Computer Science  portal for geeks.       It contains well written, well thought and well explained       computer science and programming articles, quizzes etc.'''  # Storing words in the input as a listwords = string_input.split()  # Declaring empty dictionarydictionary = {}  for word in words:      # If key is not present in the dictionary then we    # will add the key and word to the dictionary.    if (word[0] not in dictionary.keys()):          # Creating a sublist to store words with same        # key value and adding it to the list.        dictionary[word[0]] = []        dictionary[word[0]].append(word)      # If key is present then checking for the word    else:          # If word is not present in the sublist then        # adding it to the sublist of the proper key        # value        if (word not in dictionary[word[0]]):            dictionary[word[0]].append(word)  # Printing the dictionaryprint(dictionary)
Output:
{'G': ['GeeksforGeeks'], 
 'i': ['is'], 
 'a': ['a', 'and', 'articles,'], 
 'C': ['Computer'],
 'S': ['Science'], 
 'p': ['portal', 'programming'], 
 'f': ['for'], 
 'g': ['geeks.'], 
 'I': ['It'], 
 'c': ['contains', 'computer'], 
 'w': ['well', 'written,'], 
 't': ['thought'],
 'e': ['explained', 'etc.'], 
 's': ['science'], 
 'q': ['quizzes']}

You can see that in the above program’s output the words starting with G have two keys ‘G‘ and ‘g‘ so to remove that issue and make the code case-insensitive we will make use of the lower() function in python. We will save all keys with the lower case so that whenever we will check for the key both the words starting with ‘G‘ and ‘g‘ both will come under the same key. Below is the implementation:
Python3
# Python program to Create a Dictionary with Key as First# Character and Value as Words Starting with that Character  # Driver Code  # Declaring String Datastring_input = '''GeeksforGeeks is a Computer Science  portal for geeks.       It contains well written, well thought and well explained       computer science and programming articles, quizzes etc.'''  # Storing words in the input as a listwords = string_input.split()  # Declaring empty dictionarydictionary = {}  for word in words:      # If key is not present in the dictionary then we    # will add the key and word to the dictionary.    if (word[0].lower() not in dictionary.keys()):          # Creating a sublist to store words with same        # key value and adding it to the list.        dictionary[word[0].lower()] = []        dictionary[word[0].lower()].append(word)      # If key is present then checking for the word    else:          # If word is not present in the sublist then        # adding it to the sublist of the proper key        # value        if (word not in dictionary[word[0].lower()]):            dictionary[word[0].lower()].append(word)  # Printing the dictionaryprint(dictionary)
Output:
{'g': ['GeeksforGeeks', 'geeks.'],
 'i': ['is', 'It'],
 'a': ['a', 'and', 'articles,'], 
 'c': ['Computer', 'contains', 'computer'], 
 's': ['Science', 'science'], 
 'p': ['portal', 'programming'], 
 'f': ['for'], 
 'w': ['well', 'written,'], 
 't': ['thought'], 
 'e': ['explained', 'etc.'], 
 'q': ['quizzes']}

Python dictionary-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Split string into list of characters
Python | Convert a list to dictionary
How to print without newline in Python?