{"id":399602,"date":"2024-06-29T14:46:02","date_gmt":"2024-06-29T14:46:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=399602"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=399602","title":{"rendered":"<span>Dictionary\/Map<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>A basic data structure in computer science is the \u201cassociative array\u201d known as a <strong>\u201cmap\u201d.<\/strong> This structure is called a <strong>\u201cdictionary\u201d.<\/strong> Dictionaries are being used when you have key-value pairs of the information. Inputs are called keys, and outputs are called values. A dictionary is the abstract data type that can store elements so that they can be positioned quickly by using keys. Dictionary is like a container that will have a searchable assortment of items. Each item in the dictionary is stored as a key-value pair. In a dictionary, we can store multiple items with the same key.<\/p>\n<p>Dictionary consists of multiple elements in terms of key and value pair. Both key and value are considered as one single pair. This is called mapping. Elements of the dictionary are enclosed in curly brackets in terms of key and value pairs. Dictionaries enable us to work with key-value pairs. Key-value pairs are two linked values where the key is the unique identifier where we can discover our data and the value is that the information.<\/p>\n<p>Dictionary maps key-value pairs. It is a collection data type that has key-value pairs. A dictionary does not contain any duplicate members.<\/p>\n<p>It is unordered and stores data values like a map. Thus, it is similar to the real-life dictionary with distinct key values. In a dictionary, we use keys as indexes to access elements.<\/p>\n<p>The dictionary helps us to organize the collection of data. It is a special data type. Its syntax is:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"dictionary = { \u2018Key\u2019: \u2018Value\u2019 , \u2018Key\u2019 : \u2018Value\u2019 }\" alt=\"dictionary = { \u2018Key\u2019: \u2018Value\u2019 , \u2018Key\u2019 : \u2018Value\u2019 }\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/58b\/7fb\/ac8\/58b7fbac8035d2f6fd388c569d577133.svg\" width=\"314\" height=\"20\"\/><\/p>\n<p>This dictionary has two items separated by commas, and every single item is a key-value pair split up by a colon.<\/p>\n<p>Data of key must be of immutable type:<\/p>\n<ol>\n<li>\n<p>String<\/p>\n<\/li>\n<li>\n<p>Number<\/p>\n<\/li>\n<li>\n<p>Tuples<\/p>\n<\/li>\n<\/ol>\n<p>If the key is of a mutable type, it will show an error. It is similar to associative arrays in other programming languages.<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>A telephone dictionary is an example of a dictionary data structure. A telephone dictionary is organized in the form of key-value items. It contains the name of the resident and phone number. And we can call name as key and phone number as a value.<\/p>\n<p>We have another example in which the dictionary contains three items\u00a0 (key, value) pairs separated by commas <strong>(, )<\/strong> and enclosed by curly brackets. Keys are Name, John, and Department. While values are xyz, 973, and ECE.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"Dictionary = { \u201cName\u201d : \u201cxyz\u201d , \u201cJohn\u201d : 973 , \u201cDepartment\u201d: \u201cECE\u201d }\" alt=\"Dictionary = { \u201cName\u201d : \u201cxyz\u201d , \u201cJohn\u201d : 973 , \u201cDepartment\u201d: \u201cECE\u201d }\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3a5\/046\/78b\/3a504678b853d8c13823778216bc1534.svg\" width=\"495\" height=\"18\"\/><\/p>\n<h2>Operations <\/h2>\n<p>It has the following major operations:<\/p>\n<ol>\n<li>\n<p>Addition<\/p>\n<\/li>\n<li>\n<p>Modification<\/p>\n<\/li>\n<li>\n<p>Deletion<\/p>\n<\/li>\n<\/ol>\n<p><strong><em>Addition<\/em><\/strong><\/p>\n<p>Let us consider a dictionary \u2018D\u2019 having three key-value pairs.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"D = { \u2018Name\u2019 : \u2018xyz\u2019 , \u2018Number\u2019 : 578 , \u2018Branch\u2019 : \u2018ECE\u2019 }\" alt=\"D = { \u2018Name\u2019 : \u2018xyz\u2019 , \u2018Number\u2019 : 578 , \u2018Branch\u2019 : \u2018ECE\u2019 }\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/dd3\/f3f\/0de\/dd3f3f0de4b4a017f916209cd764d05a.svg\" width=\"383\" height=\"20\"\/><\/p>\n<p>In this dictionary, the keys are Name, Number, and Branch. While values are xyz, 578, and ECE. If we want to add new key-value pair to this dictionary, then we will use the following syntax:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/7b5\/81a\/4dc\/7b581a4dc096786e7b5876d85eb89fa2.png\" width=\"798\" height=\"116\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7b5\/81a\/4dc\/7b581a4dc096786e7b5876d85eb89fa2.png\"\/><figcaption><\/figcaption><\/figure>\n<p><strong><em>Modify<\/em><\/strong><\/p>\n<p>We use an existing key to modify the dictionary.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/fe7\/ce0\/ab6\/fe7ce0ab61e6df084d411ee089061559.png\" width=\"915\" height=\"68\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fe7\/ce0\/ab6\/fe7ce0ab61e6df084d411ee089061559.png\"\/><figcaption><\/figcaption><\/figure>\n<p>For dictionary D, if we want to modify the <em>\u2018Course\u2019<\/em> value with a new value, then.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c2a\/db3\/897\/c2adb389703bb528916814268818cfb4.png\" width=\"855\" height=\"94\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c2a\/db3\/897\/c2adb389703bb528916814268818cfb4.png\"\/><figcaption><\/figcaption><\/figure>\n<p><strong><em>Deletion<\/em><\/strong><\/p>\n<p>It deals with the existing item in the dictionary. For deleting one or more elements from the dictionary, we use the \u2018del keyword\u2019. Its syntax is:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/f63\/8fa\/743\/f638fa74346a98b44f6f0b17e2cb6e97.png\" width=\"797\" height=\"47\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f63\/8fa\/743\/f638fa74346a98b44f6f0b17e2cb6e97.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u00a0And <em>\u2018clear( )\u2019<\/em> function is used to clear all the key-value pairs from the dictionary. Its syntax is<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"D.clear( )\" alt=\"D.clear( )\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b4e\/1c3\/769\/b4e1c3769423eda09000b22ca1da75f0.svg\" width=\"69\" height=\"20\"\/><\/p>\n<p>Let us consider an example to comprehend the above operations. For this purpose, we consider a dictionary <strong>\u2018A\u2019 <\/strong>and enclose it in curly brackets. It contains three key-value pairs. We use the colon <strong>( : )<\/strong> operator to specify the key-value pair. And comma <strong>( )<\/strong> is used to separate multiple elements. We can access the elements by the help of keys. <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ed6\/4e3\/01f\/ed64e301f6954b638c55265383ec5cec.png\" width=\"857\" height=\"81\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ed6\/4e3\/01f\/ed64e301f6954b638c55265383ec5cec.png\"\/><figcaption><\/figcaption><\/figure>\n<p>Output will be:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"{ \u2018Bike\u2019 : \u2019Honda \u2019 , \u2018 Model \u2019 : \u2018CD70 \u2019 , \u2019Color\u2019 : \u2019Red\u2019 }\" alt=\"{ \u2018Bike\u2019 : \u2019Honda \u2019 , \u2018 Model \u2019 : \u2018CD70 \u2019 , \u2019Color\u2019 : \u2019Red\u2019 }\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/976\/d7a\/16f\/976d7a16f4ea09812a89cb0c331dd184.svg\" width=\"352\" height=\"20\"\/><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"print ( A [ \u2018Model\u2019 ]) \" alt=\"print ( A [ \u2018Model\u2019 ]) \" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/45f\/7cf\/305\/45f7cf305741a79515ac3131b02bc38d.svg\" width=\"127\" height=\"21\"\/><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"print ( A [ \u2018Color\u2019 ]) \" alt=\"print ( A [ \u2018Color\u2019 ]) \" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9c3\/223\/839\/9c3223839b54708aaa31b3c64fd3db84.svg\" width=\"121\" height=\"21\"\/><\/p>\n<p>Output will be:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"CD70\" alt=\"CD70\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/180\/40c\/963\/18040c9639081700ece0e3840d619982.svg\" width=\"42\" height=\"15\"\/><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"Red\" alt=\"Red\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fb6\/c63\/444\/fb6c6344430b64875210f3b974c17e2a.svg\" width=\"28\" height=\"15\"\/><\/p>\n<p>To add one more element:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"A[ \u2018 RegNumber \u2019] = 1257\" alt=\"A[ \u2018 RegNumber \u2019] = 1257\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/724\/145\/926\/724145926dcf88c5a18d8f115c21aca1.svg\" width=\"0\" height=\"0\"\/><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"print (A)\" alt=\"print (A)\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ddc\/4e8\/49b\/ddc4e849b430adafbbfb49d7822a9fe8.svg\" width=\"62\" height=\"20\"\/><\/p>\n<p>Output will be:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"{ \u2018Bike\u2019 : \u2019Honda \u2019 , \u2018 Model \u2019 : \u2018CD70 \u2019 , \u2019Color\u2019 : \u2019Red\u2019, \u2018 RegNumber \u2019 : 1257 }\" alt=\"{ \u2018Bike\u2019 : \u2019Honda \u2019 , \u2018 Model \u2019 : \u2018CD70 \u2019 , \u2019Color\u2019 : \u2019Red\u2019, \u2018 RegNumber \u2019 : 1257 }\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c73\/3bf\/27a\/c733bf27a9b90c82d394e43f4b38831f.svg\" width=\"502\" height=\"20\"\/><\/p>\n<p>Now, to delete one element from dictionary<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/aa3\/61b\/290\/aa361b290c7fc29f44b76835f8ed812c.png\" width=\"905\" height=\"83\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/aa3\/61b\/290\/aa361b290c7fc29f44b76835f8ed812c.png\"\/><figcaption><\/figcaption><\/figure>\n<p>Output will be:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\" { \u2018Bike\u2019 : \u2019Honda \u2019 , \u2018 Model \u2019 : \u2018CD70 \u2019 , \u2018 RegNumber \u2019 : 1257 }\" alt=\" { \u2018Bike\u2019 : \u2019Honda \u2019 , \u2018 Model \u2019 : \u2018CD70 \u2019 , \u2018 RegNumber \u2019 : 1257 }\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b56\/f0d\/386\/b56f0d38680a7c4773c28e2d19a6acad.svg\" width=\"394\" height=\"20\"\/><\/p>\n<p>To clear elements from the dictionary<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/fb8\/d5f\/5f0\/fb8d5f5f08e48df2ef153fbdc1e67705.png\" width=\"793\" height=\"104\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fb8\/d5f\/5f0\/fb8d5f5f08e48df2ef153fbdc1e67705.png\"\/><figcaption><\/figcaption><\/figure>\n<p>Output will be:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ce9\/87f\/1a4\/ce987f1a4bb48bf1ddaa19d7d922453f.png\" width=\"808\" height=\"30\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ce9\/87f\/1a4\/ce987f1a4bb48bf1ddaa19d7d922453f.png\"\/><figcaption><\/figcaption><\/figure>\n<h2>Creation of Dictionary<\/h2>\n<p>While creating a dictionary, we must know that a key can be any immutable object like numbers, strings, or tuples. Though, they cannot be objects that can be altered like lists. And keys must be unique for identifications. The vacant dictionary is created as:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/4fc\/cb2\/63b\/4fccb263b427916da8ad5e8c9a01f0d8.png\" width=\"855\" height=\"31\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4fc\/cb2\/63b\/4fccb263b427916da8ad5e8c9a01f0d8.png\"\/><figcaption><\/figcaption><\/figure>\n<p>To create a dictionary with multiple value pairs, we usually use the following syntax:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/671\/cd4\/435\/671cd4435f1a8324210fde4b03bb9b29.png\" width=\"962\" height=\"37\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/671\/cd4\/435\/671cd4435f1a8324210fde4b03bb9b29.png\"\/><figcaption><\/figcaption><\/figure>\n<p>The key and value can be any sort of data.<\/p>\n<p>Similarly, we can create a dictionary using the dictionary function. For this purpose, we can use the function\u00a0 \u2018dict ( ) \u2019. In this function, we pass the pairs of key and value in Tuple such as<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"dict ( [(K1 , V1)  , (K2,V2),\u2026\u2026., (Kn,Vn)  ]  )  \" alt=\"dict ( [(K1 , V1)  , (K2,V2),\u2026\u2026., (Kn,Vn)  ]  )  \" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/563\/aca\/c72\/563acac729e0bb70e2be2af0f381112b.svg\" width=\"314\" height=\"20\"\/><\/p>\n<p>We can also establish a dictionary by using comprehension. In this, we have to specify an expression, iteration, and condition. The syntax for creating a dictionary using comprehension is:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/4e6\/5d6\/955\/4e65d69559a8d0448a925409b0ab4e0a.png\" width=\"809\" height=\"90\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4e6\/5d6\/955\/4e65d69559a8d0448a925409b0ab4e0a.png\"\/><figcaption><\/figcaption><\/figure>\n<h2>Accessing of Dictionary Items<\/h2>\n<p>Dictionaries are enhanced to get values when the keys are known. In the dictionary, keys are used as indices. Thus, we can access the corresponding values of the dictionary with the help of keys. For example, consider a dictionary that contains 3 keys that are Name, Department, and Reg Number. With the assistance of these keys, we can access values in this dictionary.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/d56\/27f\/280\/d5627f280337ad3b86c89018c9d80235.png\" width=\"822\" height=\"235\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d56\/27f\/280\/d5627f280337ad3b86c89018c9d80235.png\"\/><figcaption><\/figcaption><\/figure>\n<p>If we try to access the key that is not available in the dictionary, there will be an error showing that the key is not found.<\/p>\n<h2>Methods In Dictionary<\/h2>\n<p>A dictionary contains key-value pair, which is available in the form of a tuple. Following are the different methods in the dictionary:<\/p>\n<p><strong><em>copy( )<\/em><\/strong><\/p>\n<p>This function is used to copy the key-value pairs into another variable.<\/p>\n<p>Let us consider a dictionary \u2018D\u2019 that contains three pairs having key and value.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b59\/ce3\/40a\/b59ce340aba433f555d000b99127bbfe.png\" width=\"825\" height=\"127\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b59\/ce3\/40a\/b59ce340aba433f555d000b99127bbfe.png\"\/><figcaption><\/figcaption><\/figure>\n<p><strong><em>clear( )<\/em><\/strong><\/p>\n<p>This function will clear all the items in the dictionary. It will give an empty dictionary at the output.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/9c9\/eb5\/d2f\/9c9eb5d2f1431e21ea7a9d6bfb6b0ed2.png\" width=\"880\" height=\"38\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9c9\/eb5\/d2f\/9c9eb5d2f1431e21ea7a9d6bfb6b0ed2.png\"\/><figcaption><\/figcaption><\/figure>\n<p><strong><em>items( )<\/em><\/strong><\/p>\n<p>It will display all the key-value pairs available in the dictionary. We will not take any argument in this. For the above dictionary \u2018D\u2019, we can write this as:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"D.items ( )\" alt=\"D.items ( )\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7af\/766\/cb0\/7af766cb0293f84af90238360f79ccb3.svg\" width=\"74\" height=\"20\"\/><\/p>\n<p><strong><em>keys( )<\/em><\/strong><\/p>\n<p>In the dictionary, the first attribute item is key, and the second is value. The purpose of a key ( ) function is to give all the keys available in the dictionary. Here we will give no argument. Its syntax is:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"D.keys ( )\" alt=\"D.keys ( )\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/462\/740\/2e1\/4627402e17bdb4db269f97552ef59471.svg\" width=\"65\" height=\"20\"\/><\/p>\n<p><strong><em>values( )<\/em><\/strong><\/p>\n<p>Its purpose is to display all the values in the dictionary. It does not require any argument. Its syntax is:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"D.values( )\" alt=\"D.values( )\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/038\/a31\/c9e\/038a31c9e6f552054fa92ad58448e8e9.svg\" width=\"79\" height=\"20\"\/><\/p>\n<p><strong><em>update( )<\/em><\/strong><\/p>\n<p>It is used to insert one or more key-value pairs into the existing dictionary. This method will take the argument to the dictionary. Its syntax is:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/7c4\/84b\/ccf\/7c484bccf784f41d80c2986d86d5f41b.png\" width=\"883\" height=\"81\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7c4\/84b\/ccf\/7c484bccf784f41d80c2986d86d5f41b.png\"\/><figcaption><\/figcaption><\/figure>\n<p><strong><em>pop( )<\/em><\/strong><\/p>\n<p>Its purpose is to remove or delete an item from the dictionary. It will return the value of a key that was deleted. Its syntax is:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/20b\/1d7\/bc7\/20b1d7bc7e284864df8bae695e23e711.png\" width=\"819\" height=\"43\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/20b\/1d7\/bc7\/20b1d7bc7e284864df8bae695e23e711.png\"\/><figcaption><\/figcaption><\/figure>\n<p>The value is an optional argument in this syntax. It means that if you only give a key as an argument, then only that argument will be deleted from the dictionary, and the corresponding value will be returned.<\/p>\n<p>Let us take a dictionary \u2018D\u2019.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c80\/f88\/276\/c80f882768ee4c465c838564159f198a.png\" width=\"857\" height=\"114\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c80\/f88\/276\/c80f882768ee4c465c838564159f198a.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u00a0And we pass the argument &#8216;Course&#8217;. Then this key argument will be deleted, and the corresponding \u2018DSP\u2019 argument will be returned.<\/p>\n<p><strong><em>popitem( )<\/em><\/strong><\/p>\n<p>It will remove the recently inserted item from the array. And the value of deleted key-value pair will be returned. It does not need any argument. For the above dictionary \u2018D\u2019, its syntax is:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"D.popitem( )\" alt=\"D.popitem( )\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bf4\/03f\/126\/bf403f12612bdd20fda14331db6964aa.svg\" width=\"91\" height=\"20\"\/><\/p>\n<p><strong><em>get( )<\/em><\/strong><\/p>\n<p>It is used to give back the value of the given key. Its syntax is:    <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b67\/5c1\/40f\/b675c140ff6027bfa886dfa39fa59bb3.png\" width=\"881\" height=\"42\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b67\/5c1\/40f\/b675c140ff6027bfa886dfa39fa59bb3.png\"\/><figcaption><\/figcaption><\/figure>\n<p>For the above dictionary D, we can write it as:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"D.get(''RegNumber'')\" alt=\"D.get(''RegNumber'')\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ad5\/e62\/22e\/ad5e6222ec5650980159354f5296f6ae.svg\" width=\"158\" height=\"21\"\/><\/p>\n<p>And it will return the value \u2018973\u2019 corresponding to \u2018RegNumber\u2019.<\/p>\n<p><strong><em>fromkeys( )<\/em><\/strong><\/p>\n<p>It will take two arguments. One is a tuple of keys. . While the next argument is the value that is applied to all the values of the tuple. The tuple is a constant, and all the keys can be formed as tuples. And these tuples can be passed as one argument. Its syntax is:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/117\/a83\/e73\/117a83e731ca106d21f841215dd54f1c.png\" width=\"915\" height=\"87\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/117\/a83\/e73\/117a83e731ca106d21f841215dd54f1c.png\"\/><figcaption><\/figcaption><\/figure>\n<p>Where \u2018t\u2019 is a tuple having keys, and &#8216;v&#8217; is the value.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"                                                                        D.fromkeys(t,0)              \" alt=\"                                                                        D.fromkeys(t,0)              \" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bc6\/d83\/798\/bc6d8379831d4063b1e6732bbef7eae9.svg\" width=\"125\" height=\"20\"\/><\/p>\n<p><strong><em>setdefault( )<\/em><\/strong><\/p>\n<p>It will take key and value as arguments. If this new key is not available in the dictionary, it will be inserted into the dictionary. If this new key is available in the dictionary, then there will be no change. Its syntax is:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e02\/bdb\/c65\/e02bdbc658f4c4cf44405c44164a428c.png\" width=\"923\" height=\"121\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e02\/bdb\/c65\/e02bdbc658f4c4cf44405c44164a428c.png\"\/><figcaption><\/figcaption><\/figure>\n<p>The output will be:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"{ \u201cName\u201d : \u201c xyz\u201d , \u201cRegNumber\u201d : 973 , \u201cCourse\u201d : \u201cDSP\u201d , \u201cAge\u201d , 23 }\" alt=\"{ \u201cName\u201d : \u201c xyz\u201d , \u201cRegNumber\u201d : 973 , \u201cCourse\u201d : \u201cDSP\u201d , \u201cAge\u201d , 23 }\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a11\/819\/1a5\/a118191a5d7436019b14ce50c73fb96e.svg\" width=\"481\" height=\"18\"\/><\/p>\n<h2>Implementation<\/h2>\n<p>To implement different methods of the dictionary, a dictionary \u2018D\u2019 will be created. Dictionary elements are key-value pairs. There are three key-value pairs. <\/p>\n<pre><code class=\"python\">D = { \"name\" : \"abc\", \"RegNumber\": 973, \"Course\": \"DSP\" } print(D); { \"name\" : \"abc\", \"RegNumber\": 973, \"Course\": \"DSP\" }  c = D.copy() \/\/create the copy of dictionary print (c) { \"name\": \"abc\", \"RegNumber\": 973, \"Course\": \"DSP\" }  c.clear() \/\/clear all key value pairs from the dictionary print(c) {}   D.items() \/\/display all the item from the dictionary dict_items([(\"name\", \"abc\"), (\"RegNumber\", 973), (\"Course\", \"DSP\")])  D.keys() \/\/to access all the keys from the dictionary dict_keys([\"name\", \"RegNumber\", \"Course\"])  D.values() \/\/to access all the values from the dictionaryprint(D) dict_values([\"abc\", 973, \"DSP\"]) print(D) { \"name\": \"abc\", \"RegNumber\": 973, \"Course\": \"DSP\" }  D.get(\"name\")  \/\/to get a value for a particular key \"abc\"  D.pop(\"Course\", \"DSP\")  \/\/to remove key value pair \"DSP\"  print(D) { \"name\": \"abc\", \"RegNumber\": 973 }  D.popitem()  \/\/to delete recently inserted item(\"RegNumber\", 973 )print(D) { \"name\": \"abc\" }  D.fromkeys( \"name\" ,\"abc\" ) { \"n\": \"abc\", \"a\": \"abc\", \"m\": \"abc\", \"e\": \"abc\" } a = (\"e\", \"b\", \"c\")  D.fromkeys(a , \"abc\") { \"e\" : \"abc\" , \"b\": \"abc\" , \"c\": \"abc\" }  print(D) { \"name\" : \"abc\" }  D.update ({ \"RegNumber\" : 973 , \"Course\" : \"DSP\"})  \/\/to update items in dictionary print(D) { \"name\" : \"abc\", \"RegNumber\" : 973  , \"Course\" : \"DSP\" }  D.setdefault(\"RegNumber\", 898 ) 973  D.pop(\"RegNumber\") 973  print(D) { \"name\": \"abc\", \"Course\" : \"DSP\" } D.setdefault( \"RegNumber\", 898 )  898  print(D) { \"name\": \"abc\" ,\"Course\" : \"DSP\", \"RegNumber\" : 898 }<\/code><\/pre>\n<h2>Code Implementation of Dictionary<\/h2>\n<p>Dictionaries play an important role in optimizing the time complexity of different tasks provided reasonable information.<\/p>\n<p><strong>Example:<\/strong> Telephone directory.<\/p>\n<p>Let we have a list of names of people and their phone numbers respectively, and we have to make a telephone directory.<\/p>\n<p><strong>Cell1:<\/strong><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/706\/070\/464\/706070464593df965ac9bdfd31d8e7a0.png\" width=\"800\" height=\"77\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/706\/070\/464\/706070464593df965ac9bdfd31d8e7a0.png\"\/><figcaption><\/figcaption><\/figure>\n<p><em>now in order to find the User4 phone number it will only take 1-time access to reach respective user&#8217; number if we will convert the lists to dictionary<\/em><\/p>\n<p><em>Lists are represented as a dictionary<\/em><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"dict={'User1':'+921','User2':'+922','User3':'+923','User4':'+924'}\" alt=\"dict={'User1':'+921','User2':'+922','User3':'+923','User4':'+924'}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a4a\/0ca\/826\/a4a0ca82649c91259e84b97a689be29b.svg\" width=\"522\" height=\"20\"\/><\/p>\n<p>Find the phone number of the fourth user<\/p>\n<pre><code class=\"python\">Search_User='User4' print (\"The phone number of \",Search_User,\"is \", dict[Search_User])<\/code><\/pre>\n<p>Output:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/82e\/b95\/dc2\/82eb95dc23e02dd3ea7f471dcc5f3c5a.png\" width=\"878\" height=\"31\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/82e\/b95\/dc2\/82eb95dc23e02dd3ea7f471dcc5f3c5a.png\"\/><figcaption><\/figcaption><\/figure>\n<p><strong>Cell2:<\/strong><\/p>\n<p><em>Adding new number and users to the telephone directory<\/em><\/p>\n<pre><code class=\"python\">dict['User5']='+925' print(dict)<\/code><\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"{'User1': '+921', 'User2': '+922', 'User3': '+923', 'User4': '+924', 'User5': '+925'}\" alt=\"{'User1': '+921', 'User2': '+922', 'User3': '+923', 'User4': '+924', 'User5': '+925'}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/596\/1b6\/f4e\/5961b6f4e16e154e9b60bfaf39c6e8c6.svg\" width=\"594\" height=\"20\"\/><\/p>\n<p><strong>Cell3:<\/strong><\/p>\n<p><em>Edit the number of a user<\/em>    <\/p>\n<pre><code class=\"python\">dict['User1']='+92111' print(dict)<\/code><\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"{'User1': '+92111', 'User2': '+922', 'User3': '+923', 'User4': '+924', 'User5': '+925'}\" alt=\"{'User1': '+92111', 'User2': '+922', 'User3': '+923', 'User4': '+924', 'User5': '+925'}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9bd\/192\/fcb\/9bd192fcb0c8e42e0c1b8dd3b34c9efc.svg\" width=\"610\" height=\"20\"\/><\/p>\n<p><strong>Cell4:<\/strong><\/p>\n<p><em>Deleting The number of a user<\/em><\/p>\n<p><em>let we want to delete the User5 phone number from our contact list<\/em><\/p>\n<pre><code class=\"python\">del dict['User5'] print(dict)<\/code><\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula\" source=\"{'User1': '+92111', 'User2': '+922', 'User3': '+923', 'User4': '+924'}\" alt=\"{'User1': '+92111', 'User2': '+922', 'User3': '+923', 'User4': '+924'}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/71e\/bcb\/e3c\/71ebcbe3c65230e63f26d67e2dab74a4.svg\" width=\"490\" height=\"20\"\/><\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/560552\/\"> https:\/\/habr.com\/ru\/articles\/560552\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>A basic data structure in computer science is the \u201cassociative array\u201d known as a <strong>\u201cmap\u201d.<\/strong> This structure is called a <strong>\u201cdictionary\u201d.<\/strong> Dictionaries are being used when you have key-value pairs of the information. Inputs are called keys, and outputs are called values. A dictionary is the abstract data type that can store elements so that they can be positioned quickly by using keys. Dictionary is like a container that will have a searchable assortment of items. Each item in the dictionary is stored as a key-value pair. In a dictionary, we can store multiple items with the same key.<\/p>\n<p>Dictionary consists of multiple elements in terms of key and value pair. Both key and value are considered as one single pair. This is called mapping. Elements of the dictionary are enclosed in curly brackets in terms of key and value pairs. Dictionaries enable us to work with key-value pairs. Key-value pairs are two linked values where the key is the unique identifier where we can discover our data and the value is that the information.<\/p>\n<p>Dictionary maps key-value pairs. It is a collection data type that has key-value pairs. A dictionary does not contain any duplicate members.<\/p>\n<p>It is unordered and stores data values like a map. Thus, it is similar to the real-life dictionary with distinct key values. In a dictionary, we use keys as indexes to access elements.<\/p>\n<p>The dictionary helps us to organize the collection of data. It is a special data type. Its syntax is:<\/p>\n<p>This dictionary has two items separated by commas, and every single item is a key-value pair split up by a colon.<\/p>\n<p>Data of key must be of immutable type:<\/p>\n<ol>\n<li>\n<p>String<\/p>\n<\/li>\n<li>\n<p>Number<\/p>\n<\/li>\n<li>\n<p>Tuples<\/p>\n<\/li>\n<\/ol>\n<p>If the key is of a mutable type, it will show an error. It is similar to associative arrays in other programming languages.<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>A telephone dictionary is an example of a dictionary data structure. A telephone dictionary is organized in the form of key-value items. It contains the name of the resident and phone number. And we can call name as key and phone number as a value.<\/p>\n<p>We have another example in which the dictionary contains three items\u00a0 (key, value) pairs separated by commas <strong>(, )<\/strong> and enclosed by curly brackets. Keys are Name, John, and Department. While values are xyz, 973, and ECE.<\/p>\n<h2>Operations <\/h2>\n<p>It has the following major operations:<\/p>\n<ol>\n<li>\n<p>Addition<\/p>\n<\/li>\n<li>\n<p>Modification<\/p>\n<\/li>\n<li>\n<p>Deletion<\/p>\n<\/li>\n<\/ol>\n<p><strong><em>Addition<\/em><\/strong><\/p>\n<p>Let us consider a dictionary \u2018D\u2019 having three key-value pairs.<\/p>\n<p>In this dictionary, the keys are Name, Number, and Branch. While values are xyz, 578, and ECE. If we want to add new key-value pair to this dictionary, then we will use the following syntax:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><strong><em>Modify<\/em><\/strong><\/p>\n<p>We use an existing key to modify the dictionary.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>For dictionary D, if we want to modify the <em>\u2018Course\u2019<\/em> value with a new value, then.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><strong><em>Deletion<\/em><\/strong><\/p>\n<p>It deals with the existing item in the dictionary. For deleting one or more elements from the dictionary, we use the \u2018del keyword\u2019. Its syntax is:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u00a0And <em>\u2018clear( )\u2019<\/em> function is used to clear all the key-value pairs from the dictionary. Its syntax is<\/p>\n<p>Let us consider an example to comprehend the above operations. For this purpose, we consider a dictionary <strong>\u2018A\u2019 <\/strong>and enclose it in curly brackets. It contains three key-value pairs. We use the colon <strong>( : )<\/strong> operator to specify the key-value pair. And comma <strong>( )<\/strong> is used to separate multiple elements. We can access the elements by the help of keys. <\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>Output will be:<\/p>\n<p>Output will be:<\/p>\n<p>To add one more element:<\/p>\n<p>Output will be:<\/p>\n<p>Now, to delete one element from dictionary<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>Output will be:<\/p>\n<p>To clear elements from the dictionary<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>Output will be:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h2>Creation of Dictionary<\/h2>\n<p>While creating a dictionary, we must know that a key can be any immutable object like numbers, strings, or tuples. Though, they cannot be objects that can be altered like lists. And keys must be unique for identifications. The vacant dictionary is created as:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>To create a dictionary with multiple value pairs, we usually use the following syntax:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>The key and value can be any sort of data.<\/p>\n<p>Similarly, we can create a dictionary using the dictionary function. For this purpose, we can use the function\u00a0 \u2018dict ( ) \u2019. In this function, we pass the pairs of key and value in Tuple such as<\/p>\n<p>We can also establish a dictionary by using comprehension. In this, we have to specify an expression, iteration, and condition. The syntax for creating a dictionary using comprehension is:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h2>Accessing of Dictionary Items<\/h2>\n<p>Dictionaries are enhanced to get values when the keys are known. In the dictionary, keys are used as indices. Thus, we can access the corresponding values of the dictionary with the help of keys. For example, consider a dictionary that contains 3 keys that are Name, Department, and Reg Number. With the assistance of these keys, we can access values in this dictionary.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>If we try to access the key that is not available in the dictionary, there will be an error showing that the key is not found.<\/p>\n<h2>Methods In Dictionary<\/h2>\n<p>A dictionary contains key-value pair, which is available in the form of a tuple. Following are the different methods in the dictionary:<\/p>\n<p><strong><em>copy( )<\/em><\/strong><\/p>\n<p>This function is used to copy the key-value pairs into another variable.<\/p>\n<p>Let us consider a dictionary \u2018D\u2019 that contains three pairs having key and value.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><strong><em>clear( )<\/em><\/strong><\/p>\n<p>This function will clear all the items in the dictionary. It will give an empty dictionary at the output.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><strong><em>items( )<\/em><\/strong><\/p>\n<p>It will display all the key-value pairs available in the dictionary. We will not take any argument in this. For the above dictionary \u2018D\u2019, we can write this as:<\/p>\n<p><strong><em>keys( )<\/em><\/strong><\/p>\n<p>In the dictionary, the first attribute item is key, and the second is value. The purpose of a key ( ) function is to give all the keys available in the dictionary. Here we will give no argument. Its syntax is:<\/p>\n<p><strong><em>values( )<\/em><\/strong><\/p>\n<p>Its purpose is to display all the values in the dictionary. It does not require any argument. Its syntax is:<\/p>\n<p><strong><em>update( )<\/em><\/strong><\/p>\n<p>It is used to insert one or more key-value pairs into the existing dictionary. This method will take the argument to the dictionary. Its syntax is:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><strong><em>pop( )<\/em><\/strong><\/p>\n<p>Its purpose is to remove or delete an item from the dictionary. It will return the value of a key that was deleted. Its syntax is:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>The value is an optional argument in this syntax. It means that if you only give a key as an argument, then only that argument will be deleted from the dictionary, and the corresponding value will be returned.<\/p>\n<p>Let us take a dictionary \u2018D\u2019.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u00a0And we pass the argument &#8216;Course&#8217;. Then this key argument will be deleted, and the corresponding \u2018DSP\u2019 argument will be returned.<\/p>\n<p><strong><em>popitem( )<\/em><\/strong><\/p>\n<p>It will remove the recently inserted item from the array. And the value of deleted key-value pair will be returned. It does not need any argument. For the above dictionary \u2018D\u2019, its syntax is:<\/p>\n<p><strong><em>get( )<\/em><\/strong><\/p>\n<p>It is used to give back the value of the given key. Its syntax is:    <\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>For the above dictionary D, we can write it as:<\/p>\n<p>And it will return the value \u2018973\u2019 corresponding to \u2018RegNumber\u2019.<\/p>\n<p><strong><em>fromkeys( )<\/em><\/strong><\/p>\n<p>It will take two arguments. One is a tuple of keys. . While the next argument is the value that is applied to all the values of the tuple. The tuple is a constant, and all the keys can be formed as tuples. And these tuples can be passed as one argument. Its syntax is:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>Where \u2018t\u2019 is a tuple having keys, and &#8216;v&#8217; is the value.<\/p>\n<p><strong><em>setdefault( )<\/em><\/strong><\/p>\n<p>It will take key and value as arguments. If this new key is not available in the dictionary, it will be inserted into the dictionary. If this new key is available in the dictionary, then there will be no change. Its syntax is:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>The output will be:<\/p>\n<h2>Implementation<\/h2>\n<p>To implement different methods of the dictionary, a dictionary \u2018D\u2019 will be created. Dictionary elements are key-value pairs. There are three key-value pairs. <\/p>\n<pre><code class=\"python\">D = { \"name\" : \"abc\", \"RegNumber\": 973, \"Course\": \"DSP\" } print(D); { \"name\" : \"abc\", \"RegNumber\": 973, \"Course\": \"DSP\" }  c = D.copy() \/\/create the copy of dictionary print (c) { \"name\": \"abc\", \"RegNumber\": 973, \"Course\": \"DSP\" }  c.clear() \/\/clear all key value pairs from the dictionary print(c) {}   D.items() \/\/display all the item from the dictionary dict_items([(\"name\", \"abc\"), (\"RegNumber\", 973), (\"Course\", \"DSP\")])  D.keys() \/\/to access all the keys from the dictionary dict_keys([\"name\", \"RegNumber\", \"Course\"])  D.values() \/\/to access all the values from the dictionaryprint(D) dict_values([\"abc\", 973, \"DSP\"]) print(D) { \"name\": \"abc\", \"RegNumber\": 973, \"Course\": \"DSP\" }  D.get(\"name\")  \/\/to get a value for a particular key \"abc\"  D.pop(\"Course\", \"DSP\")  \/\/to remove key value pair \"DSP\"  print(D) { \"name\": \"abc\", \"RegNumber\": 973 }  D.popitem()  \/\/to delete recently inserted item(\"RegNumber\", 973 )print(D) { \"name\": \"abc\" }  D.fromkeys( \"name\" ,\"abc\" ) { \"n\": \"abc\", \"a\": \"abc\", \"m\": \"abc\", \"e\": \"abc\" } a = (\"e\", \"b\", \"c\")  D.fromkeys(a , \"abc\") { \"e\" : \"abc\" , \"b\": \"abc\" , \"c\": \"abc\" }  print(D) { \"name\" : \"abc\" }  D.update ({ \"RegNumber\" : 973 , \"Course\" : \"DSP\"})  \/\/to update items in dictionary print(D) { \"name\" : \"abc\", \"RegNumber\" : 973  , \"Course\" : \"DSP\" }  D.setdefault(\"RegNumber\", 898 ) 973  D.pop(\"RegNumber\") 973  print(D) { \"name\": \"abc\", \"Course\" : \"DSP\" } D.setdefault( \"RegNumber\", 898 )  898  print(D) { \"name\": \"abc\" ,\"Course\" : \"DSP\", \"RegNumber\" : 898 }<\/code><\/pre>\n<h2>Code Implementation of Dictionary<\/h2>\n<p>Dictionaries play an important role in optimizing the time complexity of different tasks provided reasonable information.<\/p>\n<p><strong>Example:<\/strong> Telephone directory.<\/p>\n<p>Let we have a list of names of people and their phone numbers respectively, and we have to make a telephone directory.<\/p>\n<p><strong>Cell1:<\/strong><\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><em>now in order to find the User4 phone number it will only take 1-time access to reach respective user&#8217; number if we will convert the lists to dictionary<\/em><\/p>\n<p><em>Lists are represented as a dictionary<\/em><\/p>\n<p>Find the phone number of the fourth user<\/p>\n<pre><code class=\"python\">Search_User='User4' print (\"The phone number of \",Search_User,\"is \", dict[Search_User])<\/code><\/pre>\n<p>Output:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><strong>Cell2:<\/strong><\/p>\n<p><em>Adding new number and users to the telephone directory<\/em><\/p>\n<pre><code class=\"python\">dict['User5']='+925' print(dict)<\/code><\/pre>\n<p>Output:<\/p>\n<p><strong>Cell3:<\/strong><\/p>\n<p><em>Edit the number of a user<\/em>    <\/p>\n<pre><code class=\"python\">dict['User1']='+92111' print(dict)<\/code><\/pre>\n<p>Output:<\/p>\n<p><strong>Cell4:<\/strong><\/p>\n<p><em>Deleting The number of a user<\/em><\/p>\n<p><em>let we want to delete the User5 phone number from our contact list<\/em><\/p>\n<pre><code class=\"python\">del dict['User5'] print(dict)<\/code><\/pre>\n<p>Output:<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> <\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-399602","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/399602","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=399602"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/399602\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=399602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=399602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=399602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}