{"id":4904,"date":"2019-03-23T09:31:40","date_gmt":"2019-03-23T04:01:40","guid":{"rendered":"https:\/\/www.csestack.org\/?p=4904"},"modified":"2021-10-29T08:35:16","modified_gmt":"2021-10-29T03:05:16","slug":"python-dictionary","status":"publish","type":"post","link":"https:\/\/www.csestack.org\/python-dictionary\/","title":{"rendered":"[Complete Guide] Python Dictionary Tutorial for Beginners"},"content":{"rendered":"\n<p>What is a dictionary in Python? What are the commands to perform various operations on the Dictionary?<\/p>\n\n\n\n<p>The operation includes creating the dictionary, adding a new element, updating elements, deleting the dictionary.<\/p>\n\n\n\n<p>Here is a complete Python dictionary tutorial for you.<\/p>\n\n\n\n<p>At the end of this tutorial, you will also find some of the important methods you can use with the dictionary. These simple methods will make your programming go easy.<\/p>\n\n\n\n<p>Let&#8217;s start&#8230;<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69fd5c5686f48\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #000000;color:#000000\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #000000;color:#000000\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69fd5c5686f48\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.csestack.org\/python-dictionary\/#What_is_a_dictionary_in_Python\" >What is a dictionary in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.csestack.org\/python-dictionary\/#Different_Operations_on_Python_Dictionary\" >Different Operations on Python Dictionary<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.csestack.org\/python-dictionary\/#How_to_create_a_dictionary_in_Python\" >How to create a dictionary in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.csestack.org\/python-dictionary\/#How_to_access_an_element_from_the_dictionary\" >How to access an element from the dictionary?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.csestack.org\/python-dictionary\/#How_to_add_a_new_element_or_update_an_existing_element_in_the_dictionary\" >How to add a new element or update an existing element in the dictionary?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.csestack.org\/python-dictionary\/#How_to_delete_or_remove_elements_from_the_dictionary\" >How to delete or remove elements from the dictionary?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.csestack.org\/python-dictionary\/#Difference_between_del_and_pop_method_in_the_dictionary\" >Difference between del and pop method in the dictionary.<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.csestack.org\/python-dictionary\/#How_to_delete_all_the_items_in_the_dictionary\" >How to delete all the items in the dictionary?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.csestack.org\/python-dictionary\/#Dictionary_Membership_Test\" >Dictionary Membership Test<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.csestack.org\/python-dictionary\/#Iterating_Through_Python_Dictionary\" >Iterating Through Python Dictionary<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.csestack.org\/python-dictionary\/#How_to_convert_Sequence_to_Dictionary\" >How to convert Sequence to Dictionary?<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.csestack.org\/python-dictionary\/#Python_Dictionary_Methods\" >Python Dictionary Methods<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.csestack.org\/python-dictionary\/#Python_Dictionary_List_Comprehension\" >Python Dictionary List Comprehension<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.csestack.org\/python-dictionary\/#Built-in_Functions_with_Dictionary\" >Built-in Functions with Dictionary<\/a><\/li><\/ul><\/nav><\/div>\n<h3 class=\"has-text-align-center wp-block-heading\" id=\"what-is\"><span class=\"ez-toc-section\" id=\"What_is_a_dictionary_in_Python\"><\/span>What is a dictionary in Python?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>It is a data structure in Python that stores the data in key-value pairs.<\/p>\n\n\n\n<p><strong>For example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">empAge = {'Mark': 23,&nbsp; 'Bob': 45}<\/pre>\n\n\n\n<p>The <code>empAge<\/code> is a dictionary object which stores an employee&#8217;s name and age in the key-value format.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>There are two keys- &#8216;Mark&#8217; and &#8216;Bob&#8217;.<\/li><li>The value associated with key &#8216;Mark&#8217; is &#8217;23&#8217; which is his age.<\/li><li>The value associated with key Bob is &#8217;45&#8217; which is his age.<\/li><\/ul>\n\n\n\n<p><strong>Properties \/ Characteristics of dictionary:<\/strong><\/p>\n\n\n\n<p>These are the simple things you should always remember while using a Python dictionary.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Key in the dictionary should be unique. The duplicate key is not allowed.<\/li><li>Keys must be immutable (<a href=\"https:\/\/www.csestack.org\/difference-between-mutable-and-immutable-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">Mutable vs Immutable<\/a>). You can always delete and add a new key. But,&nbsp;you can not change the key.<\/li><\/ul>\n\n\n\n<h3 class=\"has-text-align-center wp-block-heading\" id=\"operations\"><span class=\"ez-toc-section\" id=\"Different_Operations_on_Python_Dictionary\"><\/span>Different Operations on Python Dictionary<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"create\"><span class=\"ez-toc-section\" id=\"How_to_create_a_dictionary_in_Python\"><\/span>How to create a dictionary in Python?<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Following is the simple syntax for creating a Python dictionary.<\/p>\n\n\n\n<p><strong>How to create an empty dictionary?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">myDict = {}<\/pre>\n\n\n\n<p>Here, <code>myDict<\/code> is the new dictionary object. You can give any name to the dictionary object.<\/p>\n\n\n\n<p><strong>How to create a dictionary with integer keys?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">empDict = {1: 'Mark', 2: 'Bob'}<\/pre>\n\n\n\n<p>Here is the <code>empDict<\/code> object which stores the employee&#8217;s ID as a key and his name as value.<\/p>\n\n\n\n<p>Employee ID is an integer (one of the <a href=\"https:\/\/www.csestack.org\/python-numeric-data-types-examples\/\" target=\"_blank\" rel=\"noreferrer noopener\">numeric data types in Python<\/a>).<\/p>\n\n\n\n<p><strong>How to create a dictionary with mixed key data types?<\/strong><\/p>\n\n\n\n<p>Instead of just using an integer value as a key we can also mix the data types of key.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">empDict = {1: 'Mark', 'salary': 200}<\/pre>\n\n\n\n<p>In the above example, the first pair of key-value has an integer key (1). The second key-value pair has a string key (&#8216;salary&#8217;).<\/p>\n\n\n\n<p><strong>How to create a dictionary using dict()?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">empDict = dict({1: 'Mark', 2: 'Bob'})<\/pre>\n\n\n\n<p>Here,&nbsp;<code>dict<\/code> is just an extra keyword. You can use it or skip it, based on how you feel comfortable with.<\/p>\n\n\n\n<p>However, as per the <a href=\"https:\/\/www.csestack.org\/c408-unnecessary-dict-list-tuple-call-rewrite-literal-python\/\">flake8 standard for writing the Python dictionary<\/a>, it is recommended to use the dictionary literal (<code>{}<\/code>) instead of <code>dict()<\/code> method.<\/p>\n\n\n\n<p>These are all different methods for creating a dictionary in Python. You can choose which suits best your requirement and feel comfortable with them.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"access-element\"><span class=\"ez-toc-section\" id=\"How_to_access_an_element_from_the_dictionary\"><\/span>How to access an element from the dictionary?<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Every key in the dictionary is mapped to the value. So, while retrieving any value from the dictionary you have to provide key elements. And you will get a value corresponding to the key in return.<\/p>\n\n\n\n<p>There are two ways of doing it.<\/p>\n\n\n\n<p><strong>Using key as indexing<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(myDict['name']) #indexing<\/pre>\n\n\n\n<p><strong>Using get method<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(myDict.get('age')) #method<\/pre>\n\n\n\n<p>Sometimes, the key you provide in the get() method may not be present. In this case, you can set the <a href=\"https:\/\/www.csestack.org\/set-default-dictionary-python-value\/\">default value for the key that is not present<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"add-update-element\"><span class=\"ez-toc-section\" id=\"How_to_add_a_new_element_or_update_an_existing_element_in_the_dictionary\"><\/span>How to add a new element or update an existing element in the dictionary?<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Let say we already have a dictionary with some elements in it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">empDict = {1: 'Mark', 2: 'Bob'}<\/pre>\n\n\n\n<p><strong>Updating existing discretionary value:<\/strong><\/p>\n\n\n\n<p>If the key is already present in the dictionary, it will overwrite the value, to preserve the uniqueness of the key.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">empDict = {1: 'Mark', 2: 'Bob'}\nempDict[2] = 'Alice'\n\nprint(empDict)\n\n# Output: {1: 'Mark', 2: 'Alice'}<\/pre><\/div>\n\n\n\n<p><strong>Adding a new element to the dictionary:<\/strong><\/p>\n\n\n\n<p>If the key is not present in the dictionary, it adds a new key-value pair in the dictionary.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">empDict = {1: 'Mark', 2: 'Bob'}\nempDict[3] ='Alice'\n\nprint(empDict)\n\n# Output: {1: 'Mark', 2: 'Bob', 3:'Alice'}<\/pre><\/div>\n\n\n\n<p>Note: The syntax for adding and updating elements in the dictionary is the same.<\/p>\n\n\n\n<p><strong>Adding a new element to the dictionary with the same key:<\/strong><\/p>\n\n\n\n<p>What if we try to add a new element in the dictionary&nbsp;with the key which is already present in the dictionary?<\/p>\n\n\n\n<p>If you try to add the same element in the dictionary with the same keys, it overwrites the key value.<\/p>\n\n\n\n<p>It is similar to updating the value of the given key in the dictionary.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"remove-element\"><span class=\"ez-toc-section\" id=\"How_to_delete_or_remove_elements_from_the_dictionary\"><\/span>How to delete or remove elements from the dictionary?<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Let&#8217;s consider this dictionary.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># create a dictionary\nsquaresDict = {1:1, 2:4, 3:9, 4:16, 5:25}<\/pre>\n\n\n\n<p><strong>How to remove a particular element from the dictionary using pop?<\/strong><\/p>\n\n\n\n<p>You have to specify the key.<\/p>\n\n\n\n<p>Here is a way of doing this using the simple pop method.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(squaresDict .pop(4))&nbsp;\n\n# Output: 16<\/pre>\n\n\n\n<p>The&nbsp;pop method returns the value of the dictionary element you are removing.<\/p>\n\n\n\n<p>When you print the dictionary after performing the pop operation, you don&#8217;t see that element in the dictionary.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(squaresDict )&nbsp;\n\n# Output: {1: 1, 2: 4, 3: 9, 5: 25}<\/pre>\n\n\n\n<p><strong>How to remove an arbitrary item for which you know key-value pair using pop?<\/strong><\/p>\n\n\n\n<p>Use <code>popitem<\/code> method for this. You need to provide both key and value here.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(squaresDict .popitem())\n# Output: (1, 1)\n\nprint(squaresDict)\n# Output: {2: 4, 3: 9, 5: 25}<\/pre>\n\n\n\n<p><strong>How to remove an element from a dictionary using del?<\/strong><\/p>\n\n\n\n<p>Just like the pop method you can also use the <code>del<\/code> object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># delete a particular item\ndel squaresDict [5]\n\nprint(squaresDict )\n# Output: {2: 4, 3: 9}<\/pre>\n\n\n\n<p>The keyword del is not only specific to the dictionary. It is a generic way of deleting any Python object.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"difference-del-pop-method\"><span class=\"ez-toc-section\" id=\"Difference_between_del_and_pop_method_in_the_dictionary\"><\/span>Difference between del and pop method in the dictionary.<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>The del object simply delete the element from the dictionary.<\/li><li>In case of the pop method, along with deleting an element from a dictionary it also returns the value of the target element.<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"delete\"><span class=\"ez-toc-section\" id=\"How_to_delete_all_the_items_in_the_dictionary\"><\/span>How to delete all the items in the dictionary?<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Now we are not interested in deleting a single element. What if you want to delete the complete dictionary? Or want to clear all the elements in the dictionary?<\/p>\n\n\n\n<p>Dictionary has a special method called clear to\u00a0delete all the items from the dictionary.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">squaresDict = {1:1, 2:4, 3:9, 4:16, 5:25}\nsquaresDict .clear() # remove all items\n\nprint(squaresDict )\n\n# Output: {}<\/pre><\/div>\n\n\n\n<p>You can see the empty dictionary as an output.<\/p>\n\n\n\n<p><strong>How to Delete Complete Dictionary?<\/strong><\/p>\n\n\n\n<p>Earlier we have tried del object for deleting a particular item from the dictionary. Now you want to delete the complete dictionary object instead of just removing all elements from the dictionary.<\/p>\n\n\n\n<p>Using a <code>del<\/code> object, you can delete the complete dictionary.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">squaresDict = {1:1, 2:4, 3:9, 4:16, 5:25}\ndel squaresDict # delete the dictionary itself<\/pre><\/div>\n\n\n\n<p>After deleting the dictionary object, when you try to print the dictionary, it throws an error.<\/p>\n\n\n\n<p><strong>Difference between del and clear method:<\/strong><\/p>\n\n\n\n<p>If you delete the dictionary using del, it wipes out the complete dictionary object.<\/p>\n\n\n\n<p>If you try to access the dictionary, it will through an error.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">empDict = {1: 'Mark', 2: 'Bob'}\ndel empDict\nprint(empDict)<\/pre><\/div>\n\n\n\n<pre class=\"wp-block-preformatted\">Traceback (most recent call last):\nFile \"\/home\/7d2f6393bbb812bde2a7f038b346bb7e.py\", line 5, in\nprint(empDict )\n<strong>NameError: name 'empDict' is not defined<\/strong><\/pre>\n\n\n\n<p>It means the dictionary has been deleted.<\/p>\n\n\n\n<p>Method <code>clear()<\/code> does not delete the dictionary. It removes all the items from the dictionary. Dictionary will be empty.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"membership-test\"><span class=\"ez-toc-section\" id=\"Dictionary_Membership_Test\"><\/span>Dictionary Membership Test<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>What is the membership test?<\/p>\n\n\n\n<p>Check if the key is present in the dictionary or not.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">squaresDict = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}<\/pre>\n\n\n\n<p>You can do the membership test using &#8216;in&#8217;.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;expected_key&gt; in &lt;dictionary_object&gt;<\/pre>\n\n\n\n<p>It returns true if the key is present in the dictionary.<\/p>\n\n\n\n<p>It returns false if the key is not present in the dictionary.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(1 in squaresDict)\n# Output: True\n\nprint(4 in squaresDict)\n# Output: False<\/pre>\n\n\n\n<p><strong>&#8220;not in&#8221;<\/strong><\/p>\n\n\n\n<p>You can also apply negation &#8216;not in&#8217;.<\/p>\n\n\n\n<p>Slightly different. If the key is not present in the dictionary, it returns True.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(2 not in squaresDict)\n# Output: True<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"iteration\"><span class=\"ez-toc-section\" id=\"Iterating_Through_Python_Dictionary\"><\/span>Iterating Through Python Dictionary<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Many times you have to iterate over all the elements in the dictionary.<\/p>\n\n\n\n<p>It is simple.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}\nfor i in squares:\n    print(squares[i])<\/pre><\/div>\n\n\n\n<p>The above example will print all the dictionary elements one-by-one.<\/p>\n\n\n\n<p>Inside for loop, you can write a code to perform any operations on each element of the dictionary.<\/p>\n\n\n\n<p>You can also use the items method for iteration.<\/p>\n\n\n\n<p>Using items methods for iterating over the dictionary.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81} \nfor item in squares.items(): \n    print(item)<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"convert-sequence\"><span class=\"ez-toc-section\" id=\"How_to_convert_Sequence_to_Dictionary\"><\/span>How to convert Sequence to Dictionary?<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p><strong>How to create a dictionary from the sequence?<\/strong><\/p>\n\n\n\n<p>You can use each sequence as a key-value pair. Here is a simple code that works for you.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">empDict = dict([(1,'Mark'), (2,'Bob')])<\/pre>\n\n\n\n<p>We are using the same <code>dict()<\/code> method that was used earlier for creating a dictionary object.<\/p>\n\n\n\n<h3 class=\"has-text-align-center wp-block-heading\" id=\"methods\"><span class=\"ez-toc-section\" id=\"Python_Dictionary_Methods\"><\/span>Python Dictionary Methods<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Look at some of the methods you can use with the dictionary.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>clear():&nbsp;<\/strong>Remove all items from the dictionary.<\/li><li><strong>copy():&nbsp;<\/strong>Return a shallow copy of the dictionary.<\/li><li><strong>fromkeys(seq[, v]):&nbsp;<\/strong>Return a new dictionary with keys from seq and value equal to v (defaults to None).<\/li><li><strong>get(key[,d]):&nbsp;<\/strong>Return the value of key. If the key does not exist, return d (defaults to None).<\/li><li><strong>items():&nbsp;<\/strong>Return a new view of the dictionary&#8217;s items (key, value).<\/li><li><strong>keys():&nbsp;<\/strong>Return a new view of the dictionary&#8217;s keys.<\/li><li><strong>pop(key[,d]):&nbsp;<\/strong>Remove the item with a key and return its value or return &#8216;d&#8217; if the key is not found. If d is not provided and the key is not found, raises KeyError.<\/li><li><strong>popitem():&nbsp;<\/strong>Remove and return an arbitrary item (key, value). Raises KeyError if the dictionary is empty.<\/li><li><strong>setdefault(key[,d]):&nbsp;<\/strong>If the key is in the dictionary, return its value. If not, insert key with a value of d and return d (defaults to None).<\/li><li><strong>update([other]):&nbsp;<\/strong>Update the dictionary with the key\/value pairs from other, overwriting existing keys.<\/li><li><strong>values():&nbsp;<\/strong>Return a new view of the dictionary&#8217;s values<\/li><\/ol>\n\n\n\n<p>Let&#8217;s take some examples of dictionary methods.<\/p>\n\n\n\n<p><strong>Write a Python code to initialize the dictionary with zero value for all specified keys.<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">marks = {}.fromkeys(['Math','English','Science'], 0)\nprint(marks)\n\n# Output: {'English': 0, 'Math': 0, 'Science': 0}<\/pre><\/div>\n\n\n\n<p><strong>Using items methods for iterating over the dictionary.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for item in marks.items():\n&nbsp; &nbsp; print(item)<\/pre>\n\n\n\n<p><strong>Using the keys method to get all the keys present in the dictionary.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">list(sorted(marks.keys()))\n\n# Output: ['English', 'Math', 'Science']<\/pre>\n\n\n\n<h3 class=\"has-text-align-center wp-block-heading\" id=\"comprehension\"><span class=\"ez-toc-section\" id=\"Python_Dictionary_List_Comprehension\"><\/span>Python Dictionary List Comprehension<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>You can use the comprehension technique to generate the dictionary items.<\/p>\n\n\n\n<p><strong>Write a python code to store the square as the value for each key.<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">squares = {x: x*x for x in range(6)}\nprint(squares)\n\n# Output: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}<\/pre><\/div>\n\n\n\n<p>This is simply equivalent to the following code.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">squares = {}\nfor x in range(6):\n    squares[x] = x*x<\/pre><\/div>\n\n\n\n<p>We can add some more conditions to the comprehension technique.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">odd_squares = {x: x*x for x in range(11) if x%2 == 1}\nprint(odd_squares)\n\n# Output: {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}<\/pre><\/div>\n\n\n\n<h3 class=\"has-text-align-center wp-block-heading\" id=\"built-in-functions\"><span class=\"ez-toc-section\" id=\"Built-in_Functions_with_Dictionary\"><\/span>Built-in Functions with Dictionary<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>There are also some built-in functions you can use with a dictionary.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>all():&nbsp;<\/strong>Return True if all keys of the dictionary are true (or if the dictionary is empty).<\/li><li><strong>any():&nbsp;<\/strong>Return True if any key of the dictionary is true. If the dictionary is empty, return False.<\/li><li><strong>len():&nbsp;<\/strong>Return the length (the number of items) in the dictionary.<\/li><li><strong>cmp():&nbsp;<\/strong>Compares items of two dictionaries.<\/li><li><strong>sorted():&nbsp;<\/strong>Return a new sorted list of keys in the dictionary.<\/li><\/ol>\n\n\n\n<p>Let&#8217;s check some examples.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}<\/pre>\n\n\n\n<p><strong>How to find the length of the dictionary (number of items in the dictionary.)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(len(squares))&nbsp;# Output: 5<\/pre>\n\n\n\n<p><strong>How to sort the items in the dictionary by key?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(sorted(squares))&nbsp;# Output: [1, 3, 5, 7, 9]<\/pre>\n\n\n\n<p><strong>Other Python Dictionary Tutorials:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.csestack.org\/set-default-dictionary-python-value\/\">Set Default Value in Dictionary<\/a><\/li><li><a href=\"https:\/\/www.csestack.org\/remove-all-0-from-a-dictionary-in-python\/\">Remove all 0 from a dictionary<\/a><\/li><\/ul>\n\n\n\n<p>This is all about the complete Python dictionary tutorial. If you have any specific questions to discuss, write our query in the comment.<\/p>\n\n\n\n<p class=\"has-text-align-center\">Happy Pythoning!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The complete guide for python dictionary. Create, add, delete elements from the dictionary, list comprehension along with built-in functions and methods explained with coding examples.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,73],"tags":[72,298],"class_list":["post-4904","post","type-post","status-publish","format-standard","hentry","category-code","category-python","tag-python","tag-python-dictionary"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Complete Guide] Python Dictionary Tutorial for Beginners<\/title>\n<meta name=\"description\" content=\"The complete guide for python dictionary. Create, add, delete elements from the dictionary, list comprehension along with built-in functions and methods explained with coding examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.csestack.org\/python-dictionary\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Complete Guide] Python Dictionary Tutorial for Beginners\" \/>\n<meta property=\"og:description\" content=\"The complete guide for python dictionary. Create, add, delete elements from the dictionary, list comprehension along with built-in functions and methods explained with coding examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.csestack.org\/python-dictionary\/\" \/>\n<meta property=\"og:site_name\" content=\"CSEstack\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/aniruddha.ca\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/aniruddha.ca\" \/>\n<meta property=\"article:published_time\" content=\"2019-03-23T04:01:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-10-29T03:05:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.csestack.org\/wp-content\/uploads\/2024\/01\/csestack-blog.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Aniruddha Chaudhari\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ani_chaudhari\" \/>\n<meta name=\"twitter:site\" content=\"@ani_chaudhari\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Aniruddha Chaudhari\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-dictionary\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-dictionary\\\/\"},\"author\":{\"name\":\"Aniruddha Chaudhari\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"headline\":\"[Complete Guide] Python Dictionary Tutorial for Beginners\",\"datePublished\":\"2019-03-23T04:01:40+00:00\",\"dateModified\":\"2021-10-29T03:05:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-dictionary\\\/\"},\"wordCount\":1659,\"commentCount\":8,\"publisher\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"keywords\":[\"Python\",\"Python Dictionary\"],\"articleSection\":[\"Code\",\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.csestack.org\\\/python-dictionary\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-dictionary\\\/\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/python-dictionary\\\/\",\"name\":\"[Complete Guide] Python Dictionary Tutorial for Beginners\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#website\"},\"datePublished\":\"2019-03-23T04:01:40+00:00\",\"dateModified\":\"2021-10-29T03:05:16+00:00\",\"description\":\"The complete guide for python dictionary. Create, add, delete elements from the dictionary, list comprehension along with built-in functions and methods explained with coding examples.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-dictionary\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.csestack.org\\\/python-dictionary\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-dictionary\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.csestack.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Complete Guide] Python Dictionary Tutorial for Beginners\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#website\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/\",\"name\":\"CSEstack\",\"description\":\"Computer Science &amp; Programming Portal\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.csestack.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\",\"name\":\"Aniruddha Chaudhari\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"contentUrl\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"width\":634,\"height\":634,\"caption\":\"Aniruddha Chaudhari\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\"},\"description\":\"I am a Python enthusiast who loves Linux and Vim. I hold a Master of Computer Science degree from NIT Trichy and have 10 years of experience in the IT industry, focusing on the Software Development Lifecycle from Requirements Gathering, Design, Development to Deployment. I have worked at IBM, Ericsson, and NetApp, and I share my knowledge on CSEstack.org.\",\"sameAs\":[\"https:\\\/\\\/www.csestack.org\",\"https:\\\/\\\/www.facebook.com\\\/aniruddha.ca\",\"pythonwithani\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/aniruddha28\\\/\",\"https:\\\/\\\/x.com\\\/ani_chaudhari\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCw0a__B0eJsvCujkSIfLTAA\"],\"url\":\"https:\\\/\\\/www.csestack.org\\\/author\\\/anicse\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Complete Guide] Python Dictionary Tutorial for Beginners","description":"The complete guide for python dictionary. Create, add, delete elements from the dictionary, list comprehension along with built-in functions and methods explained with coding examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.csestack.org\/python-dictionary\/","og_locale":"en_US","og_type":"article","og_title":"[Complete Guide] Python Dictionary Tutorial for Beginners","og_description":"The complete guide for python dictionary. Create, add, delete elements from the dictionary, list comprehension along with built-in functions and methods explained with coding examples.","og_url":"https:\/\/www.csestack.org\/python-dictionary\/","og_site_name":"CSEstack","article_publisher":"https:\/\/www.facebook.com\/aniruddha.ca","article_author":"https:\/\/www.facebook.com\/aniruddha.ca","article_published_time":"2019-03-23T04:01:40+00:00","article_modified_time":"2021-10-29T03:05:16+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/www.csestack.org\/wp-content\/uploads\/2024\/01\/csestack-blog.jpg","type":"image\/jpeg"}],"author":"Aniruddha Chaudhari","twitter_card":"summary_large_image","twitter_creator":"@ani_chaudhari","twitter_site":"@ani_chaudhari","twitter_misc":{"Written by":"Aniruddha Chaudhari","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.csestack.org\/python-dictionary\/#article","isPartOf":{"@id":"https:\/\/www.csestack.org\/python-dictionary\/"},"author":{"name":"Aniruddha Chaudhari","@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"headline":"[Complete Guide] Python Dictionary Tutorial for Beginners","datePublished":"2019-03-23T04:01:40+00:00","dateModified":"2021-10-29T03:05:16+00:00","mainEntityOfPage":{"@id":"https:\/\/www.csestack.org\/python-dictionary\/"},"wordCount":1659,"commentCount":8,"publisher":{"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"keywords":["Python","Python Dictionary"],"articleSection":["Code","Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.csestack.org\/python-dictionary\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.csestack.org\/python-dictionary\/","url":"https:\/\/www.csestack.org\/python-dictionary\/","name":"[Complete Guide] Python Dictionary Tutorial for Beginners","isPartOf":{"@id":"https:\/\/www.csestack.org\/#website"},"datePublished":"2019-03-23T04:01:40+00:00","dateModified":"2021-10-29T03:05:16+00:00","description":"The complete guide for python dictionary. Create, add, delete elements from the dictionary, list comprehension along with built-in functions and methods explained with coding examples.","breadcrumb":{"@id":"https:\/\/www.csestack.org\/python-dictionary\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.csestack.org\/python-dictionary\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.csestack.org\/python-dictionary\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.csestack.org\/"},{"@type":"ListItem","position":2,"name":"[Complete Guide] Python Dictionary Tutorial for Beginners"}]},{"@type":"WebSite","@id":"https:\/\/www.csestack.org\/#website","url":"https:\/\/www.csestack.org\/","name":"CSEstack","description":"Computer Science &amp; Programming Portal","publisher":{"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.csestack.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218","name":"Aniruddha Chaudhari","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","url":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","contentUrl":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","width":634,"height":634,"caption":"Aniruddha Chaudhari"},"logo":{"@id":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg"},"description":"I am a Python enthusiast who loves Linux and Vim. I hold a Master of Computer Science degree from NIT Trichy and have 10 years of experience in the IT industry, focusing on the Software Development Lifecycle from Requirements Gathering, Design, Development to Deployment. I have worked at IBM, Ericsson, and NetApp, and I share my knowledge on CSEstack.org.","sameAs":["https:\/\/www.csestack.org","https:\/\/www.facebook.com\/aniruddha.ca","pythonwithani","https:\/\/www.linkedin.com\/in\/aniruddha28\/","https:\/\/x.com\/ani_chaudhari","https:\/\/www.youtube.com\/channel\/UCw0a__B0eJsvCujkSIfLTAA"],"url":"https:\/\/www.csestack.org\/author\/anicse\/"}]}},"_links":{"self":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/4904","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/comments?post=4904"}],"version-history":[{"count":20,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/4904\/revisions"}],"predecessor-version":[{"id":9280,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/4904\/revisions\/9280"}],"wp:attachment":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/media?parent=4904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/categories?post=4904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/tags?post=4904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}