{"id":9045,"date":"2021-09-30T14:00:56","date_gmt":"2021-09-30T08:30:56","guid":{"rendered":"https:\/\/python-programs.com\/?p=9045"},"modified":"2021-11-22T18:34:34","modified_gmt":"2021-11-22T13:04:34","slug":"python-program-to-add-a-key-value-pair-to-the-dictionary","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-add-a-key-value-pair-to-the-dictionary\/","title":{"rendered":"Python Program to Add a Key, Value Pair to the Dictionary"},"content":{"rendered":"
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples<\/a> as it includes basic to advanced levels of concepts.<\/p>\n Dictionaries in Python:<\/strong><\/p>\n In Python, a dictionary dict is a one-to-one mapping; it includes a set of (key, value) pairs, with each key mapped to a value. It exemplifies a hash map or hash table (from Computer Science).<\/p>\n Each key denotes a value and is separated by a colon (:).<\/p>\n Curly brackets are used to define a dictionary. The value to the left of the colon is known as the key, while the value to the right of the colon is known as the value. A comma separates each (key, value) pair.<\/p>\n Example:<\/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 Example3:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to add a key, value pair to the dictionary some of them are:<\/p>\n Approach:<\/strong><\/p>\n i)Key of string datatype and value as integer datatype<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n ii)Key of integer datatype and value as integer datatype<\/strong><\/p>\n Output:<\/strong><\/p>\n iii)Key of integer datatype and value as string datatype<\/strong><\/p>\n Output:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts. Dictionaries in Python: In Python, a dictionary dict is a one-to-one mapping; it includes a set of (key, value) pairs, with each key mapped to a value. It …<\/p>\nEnter some random key = aplustopper\r\nEnter some random value of integer type = 2451<\/pre>\n
The dictionary after adding the key = aplustopper and value element = 2451 is : \r\n{'aplustopper': 2451}<\/pre>\n
Enter some random key = 752\r\nEnter some random value of integer type = 251<\/pre>\n
The dictionary after adding the key = 752 and value element = 251 is : \r\n{752: 251}<\/pre>\n
Enter some random key = 234\r\nEnter some random value of string type = btechgeeks<\/pre>\n
The dictionary after adding the key = 234 and value element = btechgeeks is : \r\n{234: 'btechgeeks'}<\/pre>\n
Program to Add a Key , Value Pair to the Dictionary<\/h2>\n
Method #1:Using update() function (User input)<\/h3>\n
\n
# given some random key\r\nkeyelement = input(\"Enter some random key = \")\r\n# given some random value of int datatype\r\nvalueelement = int(input(\"Enter some random value of integer type = \"))\r\n# Declare a dictionary and set its initial value to empty using {} or dict()\r\nsampledictionary = {}\r\n# To add the key-value pair to the dictionary, use the update() function.\r\nsampledictionary.update({keyelement: valueelement})\r\n# The Modified dictionary is printed\r\nprint(\"The dictionary after adding the key =\", keyelement,\r\n \"and value element =\", valueelement, \" is : \")\r\nprint(sampledictionary)\r\n<\/pre>\n
Enter some random key = btechgeeks\r\nEnter some random value of integer type = 2841\r\nThe dictionary after adding the key = btechgeeks and value element = 2841 is : \r\n{'btechgeeks': 2841}<\/pre>\n
# given some random key\r\nkeyelement = int(input(\"Enter some random key = \"))\r\n# given some random value of int datatype\r\nvalueelement = int(input(\"Enter some random value of integer type = \"))\r\n# Declare a dictionary and set its initial value to empty using {} or dict()\r\nsampledictionary = {}\r\n# To add the key-value pair to the dictionary, use the update() function.\r\nsampledictionary.update({keyelement: valueelement})\r\n# The Modified dictionary is printed\r\nprint(\"The dictionary after adding the key =\", keyelement,\r\n \"and value element =\", valueelement, \" is : \")\r\nprint(sampledictionary)\r\n<\/pre>\n
Enter some random key = 752\r\nEnter some random value of integer type = 251\r\nThe dictionary after adding the key = 752 and value element = 251 is : \r\n{752: 251}\r\n\r\n<\/pre>\n
# given some random key\r\nkeyelement = int(input(\"Enter some random key = \"))\r\n# given some random value of string datatype\r\nvalueelement = input(\"Enter some random value of string type = \")\r\n# Declare a dictionary and set its initial value to empty using {} or dict()\r\nsampledictionary = {}\r\n# To add the key-value pair to the dictionary, use the update() function.\r\nsampledictionary.update({keyelement: valueelement})\r\n# The Modified dictionary is printed\r\nprint(\"The dictionary after adding the key =\", keyelement,\r\n \"and value element =\", valueelement, \" is : \")\r\nprint(sampledictionary)\r\n<\/pre>\n
Enter some random key = 234\r\nEnter some random value of string type = btechgeeks\r\nThe dictionary after adding the key = 234 and value element = btechgeeks is : \r\n{234: 'btechgeeks'}\r\n\r\n<\/pre>\n
Method #2:Using [] operator(User input)<\/h3>\n
# given some random key\r\nkeyelement = input(\"Enter some random key = \")\r\n# given some random value of integer datatype\r\nvalueelement = int(input(\"Enter some random value of integer type = \"))\r\n# Declare a dictionary and set its initial value to empty using {} or dict()\r\nsampledictionary = {}\r\n# using [] function\r\nsampledictionary[keyelement] = valueelement\r\n# The Modified dictionary is printed\r\nprint(\"The dictionary after adding the key =\", keyelement,\r\n \"and value element =\", valueelement, \" is : \")\r\nprint(sampledictionary)\r\n<\/pre>\n
Enter some random key = aplustopper\r\nEnter some random value of integer type = 2451\r\nThe dictionary after adding the key = aplustopper and value element = 2451 is : \r\n{'aplustopper': 2451}<\/pre>\n
\n