Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.
Dictionaries in Python:
Dictionary is a mutable built-in Python Data Structure. It is conceptually related to List, Set, and Tuples. It is, however, indexed by keys rather than a sequence of numbers and can be thought of as associative arrays. On a high level, it consists of a key and its associated value. The Dictionary class in Python represents a hash-table implementation.
Given a string , the task is to create a Python program for creating a dictionary with the Key as the first character and the Value as words beginning with that character.
Examples:
Example1:
Input:
given string = "hello this is btechgeeks online programming platform to read the coding articles specially python language"
Output:
h ::: ['hello'] t ::: ['this', 'to', 'the'] i ::: ['is'] b ::: ['btechgeeks'] o ::: ['online'] p ::: ['programming', 'platform', 'python'] r ::: ['read'] c ::: ['coding'] a ::: ['articles'] s ::: ['specially'] l ::: ['language']
Example2:
Input:
given string = "this is btechgeeks today is monday and tomorrow is sunday sun sets in the east "
Output:
t ::: ['this', 'today', 'tomorrow', 'the'] i ::: ['is', 'in'] b ::: ['btechgeeks'] m ::: ['monday'] a ::: ['and'] s ::: ['sunday', 'sun', 'sets'] e ::: ['east']
Program to Create a Dictionary with Key as First Character and Value as Words Starting with that Character
Below are the ways to create a Python program for creating a dictionary with the Key as the first character and the Value as words beginning with that character.
- Using indexing, append ,items() function (Static Input)
- Using indexing, append ,items() function (User Input)
Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.
1)Using indexing, append ,items() function (Static Input)
Approach:
- Give the string as static input .
- Declare a dictionary which is empty using {} or dict()
- Divide the string into words using split() and save them in a list.
- Using a for loop and an if statement, determine whether the word already exists as a key in the dictionary.
- If it is not present, use the letter of the word as the key and the word as the value to create a sublist in the list and append it to it.
- If it is present, add the word to the associated sublist as the value.
- The resultant dictionary will be printed.
- Exit of Program
Below is the implementation:
# given string given_string = "hello this is btechgeeks online programming platform to read the coding articles specially python language" # Split the given string into words using split() function # Convert this into list using list() function. listString = given_string.split() # Declare a dictionary which is empty using {} or dict() resultdict = {} # Traverse the list String for stringword in listString: # checking if the first character of the word exists in dictionary resultdict keys or not if(stringword[0] not in resultdict.keys()): resultdict[stringword[0]] = [] # adding this character to the resultdict resultdict[stringword[0]].append(stringword) else: # If it is present, add the word to the associated sublist as the value. if(stringword not in resultdict[stringword[0]]): resultdict[stringword[0]].append(stringword) for key, value in resultdict.items(): print(key, ":::", value)
Output:
h ::: ['hello'] t ::: ['this', 'to', 'the'] i ::: ['is'] b ::: ['btechgeeks'] o ::: ['online'] p ::: ['programming', 'platform', 'python'] r ::: ['read'] c ::: ['coding'] a ::: ['articles'] s ::: ['specially'] l ::: ['language']
Explanation:
- A string must be entered by the user or give input as static and saved in a variable.
- It is declared that the dictionary is empty.
- The string is split down into words and kept in a list.
- To iterate through the words in the list, a for loop is utilised.
- An if statement is used to determine whether a word already exists as a key in the dictionary.
- If it is not present, the letter of the word is used as the key and the word as the value, and they are appended to the list’s sublist.
- If it is present, the word is added to the associated sublist as a value.
- The final dictionary is printed
2)Using indexing, append ,items() function (User Input)
Approach:
- Scan the given string using input() function.
- Declare a dictionary which is empty using {} or dict()
- Divide the string into words using split() and save them in a list.
- Using a for loop and an if statement, determine whether the word already exists as a key in the dictionary.
- If it is not present, use the letter of the word as the key and the word as the value to create a sublist in the list and append it to it.
- If it is present, add the word to the associated sublist as the value.
- The resultant dictionary will be printed.
- Exit of Program
Below is the implementation:
# Scanning the given string given_string = input("Enter some random string separated by spaces = ") # Split the given string into words using split() function # Convert this into list using list() function. listString = given_string.split() # Declare a dictionary which is empty using {} or dict() resultdict = {} # Traverse the list String for stringword in listString: # checking if the first character of the word exists in dictionary resultdict keys or not if(stringword[0] not in resultdict.keys()): resultdict[stringword[0]] = [] # adding this character to the resultdict resultdict[stringword[0]].append(stringword) else: # If it is present, add the word to the associated sublist as the value. if(stringword not in resultdict[stringword[0]]): resultdict[stringword[0]].append(stringword) for key, value in resultdict.items(): print(key, ":::", value)
Output:
Enter some random string separated by spaces = this is btechgeeks today is monday and tomorrow is sunday sun sets in the east t ::: ['this', 'today', 'tomorrow', 'the'] i ::: ['is', 'in'] b ::: ['btechgeeks'] m ::: ['monday'] a ::: ['and'] s ::: ['sunday', 'sun', 'sets'] e ::: ['east']
Related Programs:
- Python Program to Count the Frequency of Words Appearing in a String Using a Dictionary
- Python Program to Map Two Lists into a Dictionary
- Python Program to Generate a Dictionary that Contains Numbers (between 1 and n) in the Form (x, x*x).
- Python Program to Add a Key, Value Pair to the Dictionary
- Python Program to Sum All the Items in a Dictionary