{"id":8836,"date":"2021-09-30T14:00:50","date_gmt":"2021-09-30T08:30:50","guid":{"rendered":"https:\/\/python-programs.com\/?p=8836"},"modified":"2021-11-22T18:34:35","modified_gmt":"2021-11-22T13:04:35","slug":"python-program-to-create-a-dictionary-with-key-as-first-character-and-value-as-words-starting-with-that-character","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-create-a-dictionary-with-key-as-first-character-and-value-as-words-starting-with-that-character\/","title":{"rendered":"Python Program to Create a Dictionary with Key as First Character and Value as Words Starting with that Character"},"content":{"rendered":"
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list<\/a> is mandatory.<\/p>\n Dictionaries in Python:<\/strong><\/p>\n 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.<\/p>\n 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.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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.<\/p>\n Drive into Python Programming Examples<\/a> and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\ngiven string = \"hello this is btechgeeks online programming platform to read the coding articles specially python language\"<\/pre>\n
h ::: ['hello']\r\nt ::: ['this', 'to', 'the']\r\ni ::: ['is']\r\nb ::: ['btechgeeks']\r\no ::: ['online']\r\np ::: ['programming', 'platform', 'python']\r\nr ::: ['read']\r\nc ::: ['coding']\r\na ::: ['articles']\r\ns ::: ['specially']\r\nl ::: ['language']<\/pre>\n
given string = \"this is btechgeeks today is monday and tomorrow is sunday sun sets in the east \"<\/pre>\n
t ::: ['this', 'today', 'tomorrow', 'the']\r\ni ::: ['is', 'in']\r\nb ::: ['btechgeeks']\r\nm ::: ['monday']\r\na ::: ['and']\r\ns ::: ['sunday', 'sun', 'sets']\r\ne ::: ['east']<\/pre>\n
Program to Create a Dictionary with Key as First Character and Value as Words Starting with that Character<\/h2>\n
\n
1)Using indexing, append ,items() function (Static Input)<\/h3>\n
\n
# given string\r\ngiven_string = \"hello this is btechgeeks online programming platform to read the coding articles specially python language\"\r\n# Split the given string into words using split() function\r\n# Convert this into list using list() function.\r\nlistString = given_string.split()\r\n# Declare a dictionary which is empty using {} or dict()\r\nresultdict = {}\r\n# Traverse the list String\r\nfor stringword in listString:\r\n # checking if the first character of the word exists in dictionary resultdict keys or not\r\n if(stringword[0] not in resultdict.keys()):\r\n resultdict[stringword[0]] = []\r\n # adding this character to the resultdict\r\n resultdict[stringword[0]].append(stringword)\r\n else:\r\n # If it is present, add the word to the associated sublist as the value.\r\n if(stringword not in resultdict[stringword[0]]):\r\n resultdict[stringword[0]].append(stringword)\r\nfor key, value in resultdict.items():\r\n print(key, \":::\", value)\r\n<\/pre>\n
h ::: ['hello']\r\nt ::: ['this', 'to', 'the']\r\ni ::: ['is']\r\nb ::: ['btechgeeks']\r\no ::: ['online']\r\np ::: ['programming', 'platform', 'python']\r\nr ::: ['read']\r\nc ::: ['coding']\r\na ::: ['articles']\r\ns ::: ['specially']\r\nl ::: ['language']<\/pre>\n
\n
2)Using indexing, append ,items() function (User Input)<\/h3>\n
\n
# Scanning the given string\r\ngiven_string = input(\"Enter some random string separated by spaces = \")\r\n# Split the given string into words using split() function\r\n# Convert this into list using list() function.\r\nlistString = given_string.split()\r\n# Declare a dictionary which is empty using {} or dict()\r\nresultdict = {}\r\n# Traverse the list String\r\nfor stringword in listString:\r\n # checking if the first character of the word exists in dictionary resultdict keys or not\r\n if(stringword[0] not in resultdict.keys()):\r\n resultdict[stringword[0]] = []\r\n # adding this character to the resultdict\r\n resultdict[stringword[0]].append(stringword)\r\n else:\r\n # If it is present, add the word to the associated sublist as the value.\r\n if(stringword not in resultdict[stringword[0]]):\r\n resultdict[stringword[0]].append(stringword)\r\nfor key, value in resultdict.items():\r\n print(key, \":::\", value)\r\n<\/pre>\n
Enter some random string separated by spaces = this is btechgeeks today is monday and tomorrow is sunday sun sets in the east\r\nt ::: ['this', 'today', 'tomorrow', 'the']\r\ni ::: ['is', 'in']\r\nb ::: ['btechgeeks']\r\nm ::: ['monday']\r\na ::: ['and']\r\ns ::: ['sunday', 'sun', 'sets']\r\ne ::: ['east']<\/pre>\n
\n