{"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

Enter some random key = aplustopper\r\nEnter some random value of integer type = 2451<\/pre>\n

Output:<\/strong><\/p>\n

The dictionary after adding the key = aplustopper and value element = 2451 is : \r\n{'aplustopper': 2451}<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Enter some random key = 752\r\nEnter some random value of integer type = 251<\/pre>\n

Output:<\/strong><\/p>\n

The dictionary after adding the key = 752 and value element = 251 is : \r\n{752: 251}<\/pre>\n

Example3:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Enter some random key = 234\r\nEnter some random value of string type = btechgeeks<\/pre>\n

Output:<\/strong><\/p>\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

There are several ways to add a key, value pair to the dictionary some of them are:<\/p>\n

Method #1:Using update() function (User input)<\/h3>\n

Approach:<\/strong><\/p>\n