{"id":7732,"date":"2021-02-12T23:14:37","date_gmt":"2021-02-12T17:44:37","guid":{"rendered":"https:\/\/pynative.com\/?p=7732"},"modified":"2022-11-03T17:22:50","modified_gmt":"2022-11-03T11:52:50","slug":"python-dictionaries","status":"publish","type":"post","link":"https:\/\/pynative.com\/python-dictionaries\/","title":{"rendered":"Dictionaries in Python"},"content":{"rendered":"\n<p><strong>Dictionaries are ordered collections of unique values stored in (Key-Value) pairs.<\/strong><\/p>\n\n\n\n<p>In Python version <strong>3.7<\/strong> and onwards, dictionaries are ordered. In Python <strong>3.6<\/strong> and earlier, dictionaries are <strong>unordered<\/strong>.<\/p>\n\n\n\n<p>Python dictionary represents a mapping between a key and a value. In simple terms, a Python dictionary can store pairs of keys and values. Each key is linked to a specific value. Once stored in a dictionary, you can later obtain the value using just the key.<\/p>\n\n\n\n<p>For example, consider the Phone lookup, where it is very easy and fast to find the phone number(value) when we know the name(Key) associated with it.<\/p>\n\n\n\n<div class=\"wp-block-columns sectionpy has-black-color has-text-color has-background is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\" style=\"background-color:#edf9f8\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>Also, See<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/pynative.com\/python-dictionary-exercise-with-solutions\/\">Python Dictionary Exercise<\/a><\/li><li><a href=\"https:\/\/pynative.com\/python-dictionary-quiz\/\">Python Dictionary Quiz<\/a><\/li><li><a href=\"https:\/\/pynative.com\/python-dictionaries\/#h-summary-of-dictionary-operation\">Summary of dictionary operations<\/a><\/li><\/ul>\n<\/div>\n<\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"679\" height=\"416\" src=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/dictionaries-in-python.jpg\" alt=\"Dictionaries in Python\" class=\"wp-image-7992\" srcset=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/dictionaries-in-python.jpg 679w, https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/dictionaries-in-python-300x184.jpg 300w\" sizes=\"auto, (max-width: 679px) 100vw, 679px\" \/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-characteristics-of-dictionaries\">Characteristics of dictionaries<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Unordered<\/strong> (In Python 3.6 and lower version): The items in dictionaries are stored without any index value, which is typically a range of numbers. They are stored as Key-Value pairs, and the keys are their index, which will not be in any sequence.<\/li><li><strong>Ordered<\/strong> (In Python 3.7 and higher version): dictionaries are ordered, which means that the items have a defined order, and that order will not change. A simple Hash Table consists of key-value pair arranged in pseudo-random order based on the calculations from Hash Function.<\/li><li><strong>Unique: <\/strong>As mentioned above, each value has a Key; the Keys in Dictionaries should be\u00a0unique<em>.<\/em><strong><em>\u00a0\u00a0<\/em><\/strong>If we store any value with a Key that already exists, then the most recent value will replace the old value.<\/li><li><strong>Mutable: <\/strong>The dictionaries are changeable collections, which implies that we can add or remove items after the creation.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-yoast-seo-table-of-contents yoast-table-of-contents\"><h2>Table of contents<\/h2><ul><li><a href=\"#h-characteristics-of-dictionaries\" data-level=\"3\">Characteristics of dictionaries<\/a><\/li><li><a href=\"#h-creating-a-dictionary\" data-level=\"2\">Creating a dictionary<\/a><ul><li><a href=\"#h-empty-dictionary\" data-level=\"3\">Empty Dictionary<\/a><\/li><\/ul><\/li><li><a href=\"#h-accessing-elements-of-a-dictionary\" data-level=\"2\">Accessing elements of a dictionary<\/a><ul><li><a href=\"#h-get-all-keys-and-values\" data-level=\"3\">Get all keys and values<\/a><\/li><li><a href=\"#h-iterating-a-dictionary\" data-level=\"3\">Iterating a dictionary<\/a><\/li><\/ul><\/li><li><a href=\"#h-find-a-length-of-a-dictionary\" data-level=\"2\">Find a length of a dictionary<\/a><\/li><li><a href=\"#h-adding-items-to-the-dictionary\" data-level=\"2\">Adding items to the dictionary<\/a><ul><li><a href=\"#h-set-default-value-to-a-key\" data-level=\"3\">Set default value to a key<\/a><\/li><\/ul><\/li><li><a href=\"#h-modify-the-values-of-the-dictionary-keys\" data-level=\"2\">Modify the values of the dictionary keys<\/a><\/li><li><a href=\"#h-removing-items-from-the-dictionary\" data-level=\"2\">Removing items from the dictionary<\/a><\/li><li><a href=\"#h-checking-if-a-key-exists\" data-level=\"2\">Checking if a key exists<\/a><\/li><li><a href=\"#h-join-two-dictionary\" data-level=\"2\">Join two dictionary<\/a><ul><li><a href=\"#h-using-update-method\" data-level=\"3\">Using update() method<\/a><\/li><li><a href=\"#h-using-kwargs-to-unpack\" data-level=\"3\">Using **kwargs to unpack<\/a><\/li><li><a href=\"#h-join-two-dictionaries-having-few-items-in-common\" data-level=\"3\">Join two dictionaries having few items in common<\/a><\/li><\/ul><\/li><li><a href=\"#h-copy-a-dictionary\" data-level=\"2\">Copy a Dictionary<\/a><ul><li><a href=\"#h-copy-using-the-assignment-operator\" data-level=\"3\">Copy using the assignment operator<\/a><\/li><\/ul><\/li><li><a href=\"#h-nested-dictionary\" data-level=\"2\">Nested dictionary<\/a><ul><li><a href=\"#h-add-multiple-dictionaries-inside-a-single-dictionary\" data-level=\"3\">Add multiple dictionaries inside a single dictionary<\/a><\/li><\/ul><\/li><li><a href=\"#h-sort-dictionary\" data-level=\"2\">Sort dictionary<\/a><\/li><li><a href=\"#h-dictionary-comprehension\" data-level=\"2\">Dictionary comprehension<\/a><\/li><li><a href=\"#h-python-built-in-functions-with-dictionary\" data-level=\"2\">Python Built-in functions with dictionary<\/a><ul><li><a href=\"#h-max-and-min\" data-level=\"3\">max() and min()<\/a><\/li><li><a href=\"#h-all\" data-level=\"3\">all()<\/a><\/li><li><a href=\"#h-any\" data-level=\"3\">any()<\/a><\/li><li><a href=\"#h-when-to-use-dictionaries\" data-level=\"3\">When to use dictionaries?<\/a><\/li><\/ul><\/li><li><a href=\"#h-summary-of-dictionary-operations\" data-level=\"2\">Summary of dictionary operations<\/a><\/li><\/ul><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-creating-a-dictionary\">Creating a dictionary<\/h2>\n\n\n\n<p>There are following three ways to create a dictionary.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Using curly brackets<\/strong>: The dictionaries are created by enclosing the comma-separated Key: Value pairs inside the <code>{}<\/code> curly brackets. The colon &#8216;<code>:<\/code>&#8216; is used to separate the key and value in a pair.<\/li><li><strong>Using <code>dict()<\/code> constructor<\/strong>:&nbsp; Create a dictionary by passing the comma-separated key: value pairs inside the <code>dict()<\/code>.<\/li><li><strong>Using sequence<\/strong> having each item as a pair (key-value)<\/li><\/ul>\n\n\n\n<p>Let&#8217;s see each one of them with an example.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code1\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># create a dictionary using {}<\/span>\nperson = {<span class=\"hljs-string\">\"name\"<\/span>: <span class=\"hljs-string\">\"Jessa\"<\/span>, <span class=\"hljs-string\">\"country\"<\/span>: <span class=\"hljs-string\">\"USA\"<\/span>, <span class=\"hljs-string\">\"telephone\"<\/span>: <span class=\"hljs-number\">1178<\/span>}\nprint(person)\n<span class=\"hljs-comment\"># output {'name': 'Jessa', 'country': 'USA', 'telephone': 1178}<\/span>\n\n<span class=\"hljs-comment\"># create a dictionary using dict()<\/span>\nperson = dict({<span class=\"hljs-string\">\"name\"<\/span>: <span class=\"hljs-string\">\"Jessa\"<\/span>, <span class=\"hljs-string\">\"country\"<\/span>: <span class=\"hljs-string\">\"USA\"<\/span>, <span class=\"hljs-string\">\"telephone\"<\/span>: <span class=\"hljs-number\">1178<\/span>})\nprint(person)\n<span class=\"hljs-comment\"># output {'name': 'Jessa', 'country': 'USA', 'telephone': 1178}<\/span>\n\n<span class=\"hljs-comment\"># create a dictionary from sequence having each item as a pair<\/span>\nperson = dict(&#91;(<span class=\"hljs-string\">\"name\"<\/span>, <span class=\"hljs-string\">\"Mark\"<\/span>), (<span class=\"hljs-string\">\"country\"<\/span>, <span class=\"hljs-string\">\"USA\"<\/span>), (<span class=\"hljs-string\">\"telephone\"<\/span>, <span class=\"hljs-number\">1178<\/span>)])\nprint(person)\n\n<span class=\"hljs-comment\"># create dictionary with mixed keys keys<\/span>\n<span class=\"hljs-comment\"># first key is string and second is an integer<\/span>\nsample_dict = {<span class=\"hljs-string\">\"name\"<\/span>: <span class=\"hljs-string\">\"Jessa\"<\/span>, <span class=\"hljs-number\">10<\/span>: <span class=\"hljs-string\">\"Mobile\"<\/span>}\nprint(sample_dict)\n<span class=\"hljs-comment\"># output {'name': 'Jessa', 10: 'Mobile'}<\/span>\n\n<span class=\"hljs-comment\"># create dictionary with value as a list<\/span>\nperson = {<span class=\"hljs-string\">\"name\"<\/span>: <span class=\"hljs-string\">\"Jessa\"<\/span>, <span class=\"hljs-string\">\"telephones\"<\/span>: &#91;<span class=\"hljs-number\">1178<\/span>, <span class=\"hljs-number\">2563<\/span>, <span class=\"hljs-number\">4569<\/span>]}\nprint(person)\n<span class=\"hljs-comment\"># output {'name': 'Jessa', 'telephones': &#91;1178, 2563, 4569]}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code1', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code1');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-empty-dictionary\">Empty Dictionary<\/h3>\n\n\n\n<p>When we create a dictionary without any elements inside the curly brackets then it will be an empty dictionary.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code2\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">emptydict = {}\nprint(type(emptydict)) \n<span class=\"hljs-comment\"># Output class 'dict'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code2', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code2');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Note<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A dictionary value can be of any type, and duplicates are allowed in that.<\/li><li>Keys in the dictionary must be unique and of immutable types like string, numbers, or tuples.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-accessing-elements-of-a-dictionary\">Accessing elements of a dictionary<\/h2>\n\n\n\n<p>There are two different ways to access the elements of a dictionary.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Retrieve value using the key name inside the <code>[]<\/code> square brackets<\/li><li>Retrieve value by passing key name as a parameter to the <code>get()<\/code> method of a dictionary.<\/li><\/ol>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code3\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># create a dictionary named person<\/span>\nperson = {<span class=\"hljs-string\">\"name\"<\/span>: <span class=\"hljs-string\">\"Jessa\"<\/span>, <span class=\"hljs-string\">\"country\"<\/span>: <span class=\"hljs-string\">\"USA\"<\/span>, <span class=\"hljs-string\">\"telephone\"<\/span>: <span class=\"hljs-number\">1178<\/span>}\n\n<span class=\"hljs-comment\"># access value using key name in &#91;]<\/span>\nprint(person&#91;<span class=\"hljs-string\">'name'<\/span>])\n<span class=\"hljs-comment\"># Output 'Jessa'<\/span>\n\n<span class=\"hljs-comment\">#  get key value using key name in get()<\/span>\nprint(person.get(<span class=\"hljs-string\">'telephone'<\/span>))\n<span class=\"hljs-comment\"># Output 1178<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code3', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code3');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As we can see in the output, we retrieved the value &#8216;Jessa&#8217; using key &#8216;name&#8221; and value 1178 using its Key &#8216;telephone&#8217;.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-get-all-keys-and-values\">Get all keys and values<\/h3>\n\n\n\n<p>Use the following dictionary methods to retrieve all key and values at once<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Method<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>keys()<\/code><\/td><td>Returns the list of all keys present in the dictionary.<\/td><\/tr><tr><td><code>values()<\/code><\/td><td>Returns the list of all values present in the dictionary<\/td><\/tr><tr><td><code>items()<\/code><\/td><td>Returns all the items present in the dictionary. Each item will be inside a tuple as a key-value pair.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>We can assign each method&#8217;s output to a separate variable and use that for further computations if required.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code4\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">person = {<span class=\"hljs-string\">\"name\"<\/span>: <span class=\"hljs-string\">\"Jessa\"<\/span>, <span class=\"hljs-string\">\"country\"<\/span>: <span class=\"hljs-string\">\"USA\"<\/span>, <span class=\"hljs-string\">\"telephone\"<\/span>: <span class=\"hljs-number\">1178<\/span>}\n\n<span class=\"hljs-comment\"># Get all keys<\/span>\nprint(person.keys())\n<span class=\"hljs-comment\"># output dict_keys(&#91;'name', 'country', 'telephone'])<\/span>\nprint(type(person.keys()))\n<span class=\"hljs-comment\"># Output class 'dict_keys'<\/span>\n\n<span class=\"hljs-comment\"># Get all values<\/span>\nprint(person.values())\n<span class=\"hljs-comment\"># output dict_values(&#91;'Jessa', 'USA', 1178])<\/span>\nprint(type(person.values()))  \n<span class=\"hljs-comment\"># Output class 'dict_values'<\/span>\n\n<span class=\"hljs-comment\"># Get all key-value pair<\/span>\nprint(person.items())\n<span class=\"hljs-comment\"># output dict_items(&#91;('name', 'Jessa'), ('country', 'USA'), ('telephone', 1178)])<\/span>\nprint(type(person.items()))\n<span class=\"hljs-comment\"># Output class 'dict_items'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code4', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code4');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-iterating-a-dictionary\">Iterating a dictionary<\/h3>\n\n\n\n<p>We can iterate through a dictionary using a for-loop and access the individual keys and their corresponding values. Let us see this with an example.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code5\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">person = {<span class=\"hljs-string\">\"name\"<\/span>: <span class=\"hljs-string\">\"Jessa\"<\/span>, <span class=\"hljs-string\">\"country\"<\/span>: <span class=\"hljs-string\">\"USA\"<\/span>, <span class=\"hljs-string\">\"telephone\"<\/span>: <span class=\"hljs-number\">1178<\/span>}\n\n<span class=\"hljs-comment\"># Iterating the dictionary using for-loop<\/span>\nprint(<span class=\"hljs-string\">'key'<\/span>, <span class=\"hljs-string\">':'<\/span>, <span class=\"hljs-string\">'value'<\/span>)\n<span class=\"hljs-keyword\">for<\/span> key <span class=\"hljs-keyword\">in<\/span> person:\n    print(key, <span class=\"hljs-string\">':'<\/span>, person&#91;key])\n\n<span class=\"hljs-comment\"># using items() method<\/span>\nprint(<span class=\"hljs-string\">'key'<\/span>, <span class=\"hljs-string\">':'<\/span>, <span class=\"hljs-string\">'value'<\/span>)\n<span class=\"hljs-keyword\">for<\/span> key_value <span class=\"hljs-keyword\">in<\/span> person.items():\n    <span class=\"hljs-comment\"># first is key, and second is value<\/span>\n    print(key_value&#91;<span class=\"hljs-number\">0<\/span>], key_value&#91;<span class=\"hljs-number\">1<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code5', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code5');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">key : value\nname : Jessa\ncountry : USA\ntelephone : 1178\n\nkey : value\nname Jessa\ncountry USA\ntelephone 1178<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-find-a-length-of-a-dictionary\">Find a length of a dictionary<\/h2>\n\n\n\n<p>In order to find the number of items in a dictionary, we can use the <code>len()<\/code> function. Let us consider the personal details dictionary which we created in the above example and find its length.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code6\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">person = {<span class=\"hljs-string\">\"name\"<\/span>: <span class=\"hljs-string\">\"Jessa\"<\/span>, <span class=\"hljs-string\">\"country\"<\/span>: <span class=\"hljs-string\">\"USA\"<\/span>, <span class=\"hljs-string\">\"telephone\"<\/span>: <span class=\"hljs-number\">1178<\/span>}\n\n<span class=\"hljs-comment\"># count number of keys present in  a dictionary<\/span>\nprint(len(person))  \n<span class=\"hljs-comment\"># output 3<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code6', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code6');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-adding-items-to-the-dictionary\">Adding items to the dictionary<\/h2>\n\n\n\n<p>We can add new items to the dictionary using the following two ways.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Using key-value assignment: <\/strong>Using a simple assignment statement where value can be assigned directly to the new key.<\/li><li><strong>Using update() Method: <\/strong>In this method, the item passed inside the update() method will be inserted into the dictionary. The item can be another dictionary or any iterable like a tuple of key-value pairs.<\/li><\/ul>\n\n\n\n<p>Now, Let&#8217;s see how to add two new keys to the dictionary.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code7\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">person = {<span class=\"hljs-string\">\"name\"<\/span>: <span class=\"hljs-string\">\"Jessa\"<\/span>, <span class=\"hljs-string\">'country'<\/span>: <span class=\"hljs-string\">\"USA\"<\/span>, <span class=\"hljs-string\">\"telephone\"<\/span>: <span class=\"hljs-number\">1178<\/span>}\n\n<span class=\"hljs-comment\"># update dictionary by adding 2 new keys<\/span>\nperson&#91;<span class=\"hljs-string\">\"weight\"<\/span>] = <span class=\"hljs-number\">50<\/span>\nperson.update({<span class=\"hljs-string\">\"height\"<\/span>: <span class=\"hljs-number\">6<\/span>})\n\n<span class=\"hljs-comment\"># print the updated dictionary<\/span>\nprint(person)\n<span class=\"hljs-comment\"># output {'name': 'Jessa', 'country': 'USA', 'telephone': 1178, 'weight': 50, 'height': 6}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code7', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code7');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Note<\/strong>: We can also add more than one key using the update() method.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code8\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">person = {<span class=\"hljs-string\">\"name\"<\/span>: <span class=\"hljs-string\">\"Jessa\"<\/span>, <span class=\"hljs-string\">'country'<\/span>: <span class=\"hljs-string\">\"USA\"<\/span>}\n\n<span class=\"hljs-comment\"># Adding 2 new keys at once<\/span>\n<span class=\"hljs-comment\"># pass new keys as dict<\/span>\nperson.update({<span class=\"hljs-string\">\"weight\"<\/span>: <span class=\"hljs-number\">50<\/span>, <span class=\"hljs-string\">\"height\"<\/span>: <span class=\"hljs-number\">6<\/span>})\n<span class=\"hljs-comment\"># print the updated dictionary<\/span>\nprint(person)\n<span class=\"hljs-comment\"># output {'name': 'Jessa', 'country': 'USA', 'weight': 50, 'height': 6}<\/span>\n\n<span class=\"hljs-comment\"># pass new keys as as list of tuple<\/span>\nperson.update(&#91;(<span class=\"hljs-string\">\"city\"<\/span>, <span class=\"hljs-string\">\"Texas\"<\/span>), (<span class=\"hljs-string\">\"company\"<\/span>, <span class=\"hljs-string\">\"Google\"<\/span>,)])\n<span class=\"hljs-comment\"># print the updated dictionary<\/span>\nprint(person)\n<span class=\"hljs-comment\"># output {'name': 'Jessa', 'country': 'USA', 'weight': 50, 'height': 6, 'city': 'Texas', 'company': 'Google'}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code8', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code8');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-set-default-value-to-a-key\">Set default value to a key<\/h3>\n\n\n\n<p>Using the <code>setdefault()<\/code> method default value can be assigned to a key in the dictionary. In case the key doesn\u2019t exist already, then the key will be inserted into the dictionary, and the value becomes the default value, and None will be inserted if a value is not mentioned.<\/p>\n\n\n\n<p>In case the key exists, then it will return the value of a key.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code9\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">person_details = {<span class=\"hljs-string\">\"name\"<\/span>: <span class=\"hljs-string\">\"Jessa\"<\/span>, <span class=\"hljs-string\">\"country\"<\/span>: <span class=\"hljs-string\">\"USA\"<\/span>, <span class=\"hljs-string\">\"telephone\"<\/span>: <span class=\"hljs-number\">1178<\/span>}\n\n<span class=\"hljs-comment\"># set default value if key doesn't exists<\/span>\nperson_details.setdefault(<span class=\"hljs-string\">'state'<\/span>, <span class=\"hljs-string\">'Texas'<\/span>)\n\n<span class=\"hljs-comment\"># key doesn't exists and value not mentioned. default None<\/span>\nperson_details.setdefault(<span class=\"hljs-string\">\"zip\"<\/span>)\n\n<span class=\"hljs-comment\"># key exists and value mentioned. doesn't  change value<\/span>\nperson_details.setdefault(<span class=\"hljs-string\">'country'<\/span>, <span class=\"hljs-string\">'Canada'<\/span>)\n\n<span class=\"hljs-comment\"># Display dictionary<\/span>\n<span class=\"hljs-keyword\">for<\/span> key, value <span class=\"hljs-keyword\">in<\/span> person_details.items():\n    print(key, <span class=\"hljs-string\">':'<\/span>, value)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code9', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code9');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">name : Jessa\ncountry : USA\ntelephone : 1178\nstate : Texas\nzip : None<\/pre>\n\n\n\n<p><strong>Note<\/strong>: As seen in the above example the value of the setdefault() method has no effect on the &#8216;country&#8217; as the key already exists.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-modify-the-values-of-the-dictionary-keys\">Modify the values of the dictionary keys<\/h2>\n\n\n\n<p>We can modify the values of the existing dictionary keys using the following two ways.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Using key name<\/strong>: We can directly assign new values by using its key name. The key name will be the existing one and we can mention the new value.<\/li><li><strong>Using update() method<\/strong>: We can use the update method by passing the key-value pair to change the value. Here the key name will be the existing one, and the value to be updated will be new.<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code10\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">person = {<span class=\"hljs-string\">\"name\"<\/span>: <span class=\"hljs-string\">\"Jessa\"<\/span>, <span class=\"hljs-string\">\"country\"<\/span>: <span class=\"hljs-string\">\"USA\"<\/span>}\n\n<span class=\"hljs-comment\"># updating the country name<\/span>\nperson&#91;<span class=\"hljs-string\">\"country\"<\/span>] = <span class=\"hljs-string\">\"Canada\"<\/span>\n<span class=\"hljs-comment\"># print the updated country<\/span>\nprint(person&#91;<span class=\"hljs-string\">'country'<\/span>])\n<span class=\"hljs-comment\"># Output 'Canada'<\/span>\n\n<span class=\"hljs-comment\"># updating the country name using update() method<\/span>\nperson.update({<span class=\"hljs-string\">\"country\"<\/span>: <span class=\"hljs-string\">\"USA\"<\/span>})\n<span class=\"hljs-comment\"># print the updated country<\/span>\nprint(person&#91;<span class=\"hljs-string\">'country'<\/span>])\n<span class=\"hljs-comment\"># Output 'USA'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code10', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code10');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-removing-items-from-the-dictionary\">Removing items from the dictionary<\/h2>\n\n\n\n<p>There are several methods to remove items from the dictionary. Whether we want to remove the single item or the last inserted item or delete the entire dictionary, we can choose the method to be used.<\/p>\n\n\n\n<p>Use the following dictionary methods to remove keys from a dictionary.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Method<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>pop(key[,d])<\/code><\/td><td>Return and removes the item with the <code>key<\/code> and return its value. If the <code>key<\/code> is not found, it raises <code>KeyError<\/code>.<\/td><\/tr><tr><td><code>popitem()<\/code><\/td><td>Return and removes the last inserted item from the dictionary. If the dictionary is empty, it raises <code>KeyErr<\/code>or.<\/td><\/tr><tr><td><code>del key<\/code><\/td><td>The <code>del<\/code> keyword will delete the item with the key that is passed<\/td><\/tr><tr><td><code>clear()<\/code><\/td><td>Removes all items from the dictionary. Empty the dictionary<\/td><\/tr><tr><td><code>del dict_name<\/code><\/td><td>Delete the entire dictionary<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Now, Let&#8217;s see how to delete items from a dictionary with an example.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code11\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">person = {<span class=\"hljs-string\">'name'<\/span>: <span class=\"hljs-string\">'Jessa'<\/span>, <span class=\"hljs-string\">'country'<\/span>: <span class=\"hljs-string\">'USA'<\/span>, <span class=\"hljs-string\">'telephone'<\/span>: <span class=\"hljs-number\">1178<\/span>, <span class=\"hljs-string\">'weight'<\/span>: <span class=\"hljs-number\">50<\/span>, <span class=\"hljs-string\">'height'<\/span>: <span class=\"hljs-number\">6<\/span>}\n\n<span class=\"hljs-comment\"># Remove last inserted item from the dictionary<\/span>\ndeleted_item = person.popitem()\nprint(deleted_item)  <span class=\"hljs-comment\"># output ('height', 6)<\/span>\n<span class=\"hljs-comment\"># display updated dictionary<\/span>\nprint(person)  \n<span class=\"hljs-comment\"># Output {'name': 'Jessa', 'country': 'USA', 'telephone': 1178, 'weight': 50}<\/span>\n\n<span class=\"hljs-comment\"># Remove key 'telephone' from the dictionary<\/span>\ndeleted_item = person.pop(<span class=\"hljs-string\">'telephone'<\/span>)\nprint(deleted_item)  <span class=\"hljs-comment\"># output 1178<\/span>\n<span class=\"hljs-comment\"># display updated dictionary<\/span>\nprint(person)  \n<span class=\"hljs-comment\"># Output {'name': 'Jessa', 'country': 'USA', 'weight': 50}<\/span>\n\n<span class=\"hljs-comment\"># delete key 'weight'<\/span>\n<span class=\"hljs-keyword\">del<\/span> person&#91;<span class=\"hljs-string\">'weight'<\/span>]\n<span class=\"hljs-comment\"># display updated dictionary<\/span>\nprint(person)\n<span class=\"hljs-comment\"># Output {'name': 'Jessa', 'country': 'USA'}<\/span>\n\n<span class=\"hljs-comment\"># remove all item (key-values) from dict<\/span>\nperson.clear()\n<span class=\"hljs-comment\"># display updated dictionary<\/span>\nprint(person)  <span class=\"hljs-comment\"># {}<\/span>\n\n<span class=\"hljs-comment\"># Delete the entire dictionary<\/span>\n<span class=\"hljs-keyword\">del<\/span> person<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code11', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code11');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-checking-if-a-key-exists\">Checking if a key exists<\/h2>\n\n\n\n<p>In order to check whether a particular key exists in a dictionary, we can use the&nbsp;<code>keys()<\/code>&nbsp;method and <code>in<\/code> operator. We can use the <code>in<\/code> operator to check whether the key is present in the list of keys returned by the <code>keys()<\/code> method.<\/p>\n\n\n\n<p>In this method, we can just check whether our key is present in the list of keys that will be returned from the <code>keys()<\/code> method.<\/p>\n\n\n\n<p>Let&#8217;s check whether the &#8216;country&#8217; key exists and prints its value if found.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code12\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">person = {<span class=\"hljs-string\">'name'<\/span>: <span class=\"hljs-string\">'Jessa'<\/span>, <span class=\"hljs-string\">'country'<\/span>: <span class=\"hljs-string\">'USA'<\/span>, <span class=\"hljs-string\">'telephone'<\/span>: <span class=\"hljs-number\">1178<\/span>}\n\n<span class=\"hljs-comment\"># Get the list of keys and check if 'country' key is present<\/span>\nkey_name = <span class=\"hljs-string\">'country'<\/span>\n<span class=\"hljs-keyword\">if<\/span> key_name <span class=\"hljs-keyword\">in<\/span> person.keys():\n    print(<span class=\"hljs-string\">\"country name is\"<\/span>, person&#91;key_name])\n<span class=\"hljs-keyword\">else<\/span>:\n    print(<span class=\"hljs-string\">\"Key not found\"<\/span>)\n<span class=\"hljs-comment\"># Output country name is USA<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code12', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code12');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-join-two-dictionary\">Join two dictionary<\/h2>\n\n\n\n<p>We can add two dictionaries using the <code>update()<\/code> method or unpacking arbitrary keywords operator <code>**<\/code>. Let us see each one with an example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-using-update-method\">Using update() method<\/h3>\n\n\n\n<p>In this method, the dictionary to be added will be passed as the argument to the update() method and the updated dictionary will have items of both the dictionaries.<\/p>\n\n\n\n<p>Let&#8217;s see how to merge the second dictionary into the first dictionary<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code13\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">dict1 = {<span class=\"hljs-string\">'Jessa'<\/span>: <span class=\"hljs-number\">70<\/span>, <span class=\"hljs-string\">'Arul'<\/span>: <span class=\"hljs-number\">80<\/span>, <span class=\"hljs-string\">'Emma'<\/span>: <span class=\"hljs-number\">55<\/span>}\ndict2 = {<span class=\"hljs-string\">'Kelly'<\/span>: <span class=\"hljs-number\">68<\/span>, <span class=\"hljs-string\">'Harry'<\/span>: <span class=\"hljs-number\">50<\/span>, <span class=\"hljs-string\">'Olivia'<\/span>: <span class=\"hljs-number\">66<\/span>}\n\n<span class=\"hljs-comment\"># copy second dictionary into first dictionary<\/span>\ndict1.update(dict2)\n<span class=\"hljs-comment\"># printing the updated dictionary<\/span>\nprint(dict1)\n<span class=\"hljs-comment\"># output {'Jessa': 70, 'Arul': 80, 'Emma': 55, 'Kelly': 68, 'Harry': 50, 'Olivia': 66}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code13', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code13');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As seen in the above example the items of both the dictionaries have been updated and the dict1 will have the items from both the dictionaries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-using-kwargs-to-unpack\">Using <code>**kwargs<\/code> to unpack<\/h3>\n\n\n\n<p>We can unpack any number of dictionary and add their contents to another dictionary using <code>**kwargs<\/code>. In this way, we can add multiple length arguments to one dictionary in a single statement.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code14\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">student_dict1 = {<span class=\"hljs-string\">'Aadya'<\/span>: <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'Arul'<\/span>: <span class=\"hljs-number\">2<\/span>, }\nstudent_dict2 = {<span class=\"hljs-string\">'Harry'<\/span>: <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-string\">'Olivia'<\/span>: <span class=\"hljs-number\">6<\/span>}\nstudent_dict3 = {<span class=\"hljs-string\">'Nancy'<\/span>: <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-string\">'Perry'<\/span>: <span class=\"hljs-number\">9<\/span>}\n\n<span class=\"hljs-comment\"># join three dictionaries <\/span>\nstudent_dict = {**student_dict1, **student_dict2, **student_dict3}\n<span class=\"hljs-comment\"># printing the final Merged dictionary<\/span>\nprint(student_dict)\n<span class=\"hljs-comment\"># Output {'Aadya': 1, 'Arul': 2, 'Harry': 5, 'Olivia': 6, 'Nancy': 7, 'Perry': 9}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code14', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code14');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As seen in the above example the values of all the dictionaries have been merged into one.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-join-two-dictionaries-having-few-items-in-common\">Join two dictionaries having few items in common<\/h3>\n\n\n\n<p><strong>Note<\/strong>: One thing to note here is that if both the dictionaries have a common key then the first dictionary value will be overridden with the second dictionary value.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code15\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">dict1 = {<span class=\"hljs-string\">'Jessa'<\/span>: <span class=\"hljs-number\">70<\/span>, <span class=\"hljs-string\">'Arul'<\/span>: <span class=\"hljs-number\">80<\/span>, <span class=\"hljs-string\">'Emma'<\/span>: <span class=\"hljs-number\">55<\/span>}\ndict2 = {<span class=\"hljs-string\">'Kelly'<\/span>: <span class=\"hljs-number\">68<\/span>, <span class=\"hljs-string\">'Harry'<\/span>: <span class=\"hljs-number\">50<\/span>, <span class=\"hljs-string\">'Emma'<\/span>: <span class=\"hljs-number\">66<\/span>}\n\n<span class=\"hljs-comment\"># join two dictionaries with some common items<\/span>\ndict1.update(dict2)\n<span class=\"hljs-comment\"># printing the updated dictionary<\/span>\nprint(dict1&#91;<span class=\"hljs-string\">'Emma'<\/span>])\n<span class=\"hljs-comment\"># Output 66<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code15', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code15');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As mentioned in the case of the same key in two dictionaries the latest one will override the old one.<\/p>\n\n\n\n<p>In the above example, both the dictionaries have the key &#8216;Emma&#8217; So the value of the second dictionary is used in the final merged dictionary <code>dict1<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-copy-a-dictionary\">Copy a Dictionary<\/h2>\n\n\n\n<p>We can create a copy of a dictionary using the following two ways<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Using <code>copy()<\/code> method.<\/li><li>Using the <code>dict()<\/code> constructor<\/li><\/ul>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code16\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">dict1 = {<span class=\"hljs-string\">'Jessa'<\/span>: <span class=\"hljs-number\">70<\/span>, <span class=\"hljs-string\">'Emma'<\/span>: <span class=\"hljs-number\">55<\/span>}\n\n<span class=\"hljs-comment\"># Copy dictionary using copy() method<\/span>\ndict2 = dict1.copy()\n<span class=\"hljs-comment\"># printing the new  dictionary<\/span>\nprint(dict2)\n<span class=\"hljs-comment\"># output {'Jessa': 70, 'Emma': 55}<\/span>\n\n<span class=\"hljs-comment\"># Copy dictionary using dict() constructor<\/span>\ndict3 = dict(dict1)\nprint(dict3)\n<span class=\"hljs-comment\"># output {'Jessa': 70, 'Emma': 55}<\/span>\n\n<span class=\"hljs-comment\"># Copy dictionary using the output of items() methods<\/span>\ndict4 = dict(dict1.items())\nprint(dict4)\n<span class=\"hljs-comment\"># output {'Jessa': 70, 'Emma': 55}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code16', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code16');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-copy-using-the-assignment-operator\">Copy using the assignment operator<\/h3>\n\n\n\n<p>We can simply use the <code>'='<\/code> operator to create a copy.<\/p>\n\n\n\n<p>Note: When you set <code>dict2 = dict1<\/code>, you are making them refer to the same dict object, so when you modify one of them, all references associated with that object reflect the current state of the object. So don&#8217;t use the assignment operator to copy the dictionary instead use the <code>copy()<\/code> method.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code17\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">dict1 = {<span class=\"hljs-string\">'Jessa'<\/span>: <span class=\"hljs-number\">70<\/span>, <span class=\"hljs-string\">'Emma'<\/span>: <span class=\"hljs-number\">55<\/span>}\n\n<span class=\"hljs-comment\"># Copy dictionary using assignment = operator<\/span>\ndict2 = dict1\n<span class=\"hljs-comment\"># modify dict2<\/span>\ndict2.update({<span class=\"hljs-string\">'Jessa'<\/span>: <span class=\"hljs-number\">90<\/span>})\nprint(dict2)\n<span class=\"hljs-comment\"># Output {'Jessa': 90, 'Emma': 55}<\/span>\n\nprint(dict1)\n<span class=\"hljs-comment\"># Output {'Jessa': 90, 'Emma': 55}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code17', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code17');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-nested-dictionary\">Nested dictionary<\/h2>\n\n\n\n<p>Nested dictionaries are dictionaries that have one or more dictionaries as their members. It is a collection of many dictionaries in one dictionary.<\/p>\n\n\n\n<p>Let us see an example of creating a nested dictionary &#8216;Address&#8217; inside a &#8216;person&#8217; dictionary.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code18\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># address dictionary to store person address<\/span>\naddress = {<span class=\"hljs-string\">\"state\"<\/span>: <span class=\"hljs-string\">\"Texas\"<\/span>, <span class=\"hljs-string\">'city'<\/span>: <span class=\"hljs-string\">'Houston'<\/span>}\n\n<span class=\"hljs-comment\"># dictionary to store person details with address as a nested dictionary<\/span>\nperson = {<span class=\"hljs-string\">'name'<\/span>: <span class=\"hljs-string\">'Jessa'<\/span>, <span class=\"hljs-string\">'company'<\/span>: <span class=\"hljs-string\">'Google'<\/span>, <span class=\"hljs-string\">'address'<\/span>: address}\n\n<span class=\"hljs-comment\"># Display dictionary<\/span>\nprint(<span class=\"hljs-string\">\"person:\"<\/span>, person)\n\n<span class=\"hljs-comment\"># Get nested dictionary key 'city'<\/span>\nprint(<span class=\"hljs-string\">\"City:\"<\/span>, person&#91;<span class=\"hljs-string\">'address'<\/span>]&#91;<span class=\"hljs-string\">'city'<\/span>])\n\n<span class=\"hljs-comment\"># Iterating outer dictionary<\/span>\nprint(<span class=\"hljs-string\">\"Person details\"<\/span>)\n<span class=\"hljs-keyword\">for<\/span> key, value <span class=\"hljs-keyword\">in<\/span> person.items():\n    <span class=\"hljs-keyword\">if<\/span> key == <span class=\"hljs-string\">'address'<\/span>:\n        <span class=\"hljs-comment\"># Iterating through nested dictionary<\/span>\n        print(<span class=\"hljs-string\">\"Person Address\"<\/span>)\n        <span class=\"hljs-keyword\">for<\/span> nested_key, nested_value <span class=\"hljs-keyword\">in<\/span> value.items():\n            print(nested_key, <span class=\"hljs-string\">':'<\/span>, nested_value)\n    <span class=\"hljs-keyword\">else<\/span>:\n        print(key, <span class=\"hljs-string\">':'<\/span>, value)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code18', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code18');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">person: {'name': 'Jessa', 'company': 'Google', 'address': {'state': 'Texas', 'city': 'Houston'}}\n\nCity: Houston\n\nPerson details\nname: Jessa\ncompany: Google\n\nPerson Address\nstate: Texas\ncity : Houston<\/pre>\n\n\n\n<p>As we can see in the output we have added one dictionary inside another dictionary.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-add-multiple-dictionaries-inside-a-single-dictionary\">Add multiple dictionaries inside a single dictionary<\/h3>\n\n\n\n<p>Let us see an example of creating multiple nested dictionaries inside a single dictionary.<\/p>\n\n\n\n<p>In this example, we will create a separate dictionary for each student and in the end, we will add each student to the &#8216;class_six&#8217; dictionary. So each student is nothing but a key in a &#8216;class_six&#8217; dictionary.<\/p>\n\n\n\n<p>In order to access the nested dictionary values, we have to pass the outer dictionary key, followed by the individual dictionary key.<\/p>\n\n\n\n<p>For example, <code>class_six['student3']['name']<\/code><\/p>\n\n\n\n<p>We can iterate through the individual member dictionaries using nested for-loop with the outer loop for the outer dictionary and inner loop for retrieving the members of the collection.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code19\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># each dictionary will store data of a single student<\/span>\njessa = {<span class=\"hljs-string\">'name'<\/span>: <span class=\"hljs-string\">'Jessa'<\/span>, <span class=\"hljs-string\">'state'<\/span>: <span class=\"hljs-string\">'Texas'<\/span>, <span class=\"hljs-string\">'city'<\/span>: <span class=\"hljs-string\">'Houston'<\/span>, <span class=\"hljs-string\">'marks'<\/span>: <span class=\"hljs-number\">75<\/span>}\nemma = {<span class=\"hljs-string\">'name'<\/span>: <span class=\"hljs-string\">'Emma'<\/span>, <span class=\"hljs-string\">'state'<\/span>: <span class=\"hljs-string\">'Texas'<\/span>, <span class=\"hljs-string\">'city'<\/span>: <span class=\"hljs-string\">'Dallas'<\/span>, <span class=\"hljs-string\">'marks'<\/span>: <span class=\"hljs-number\">60<\/span>}\nkelly = {<span class=\"hljs-string\">'name'<\/span>: <span class=\"hljs-string\">'Kelly'<\/span>, <span class=\"hljs-string\">'state'<\/span>: <span class=\"hljs-string\">'Texas'<\/span>, <span class=\"hljs-string\">'city'<\/span>: <span class=\"hljs-string\">'Austin'<\/span>, <span class=\"hljs-string\">'marks'<\/span>: <span class=\"hljs-number\">85<\/span>}\n\n<span class=\"hljs-comment\"># Outer dictionary to store all student dictionaries (nested dictionaries)<\/span>\nclass_six = {<span class=\"hljs-string\">'student1'<\/span>: jessa, <span class=\"hljs-string\">'student2'<\/span>: emma, <span class=\"hljs-string\">'student3'<\/span>: kelly}\n\n<span class=\"hljs-comment\"># Get student3's name and mark<\/span>\nprint(<span class=\"hljs-string\">\"Student 3 name:\"<\/span>, class_six&#91;<span class=\"hljs-string\">'student3'<\/span>]&#91;<span class=\"hljs-string\">'name'<\/span>])\nprint(<span class=\"hljs-string\">\"Student 3 marks:\"<\/span>, class_six&#91;<span class=\"hljs-string\">'student3'<\/span>]&#91;<span class=\"hljs-string\">'marks'<\/span>])\n\n<span class=\"hljs-comment\"># Iterating outer dictionary<\/span>\nprint(<span class=\"hljs-string\">\"\\nClass details\\n\"<\/span>)\n<span class=\"hljs-keyword\">for<\/span> key, value <span class=\"hljs-keyword\">in<\/span> class_six.items():\n    <span class=\"hljs-comment\"># Iterating through nested dictionary<\/span>\n    <span class=\"hljs-comment\"># Display each student data<\/span>\n    print(key)\n    <span class=\"hljs-keyword\">for<\/span> nested_key, nested_value <span class=\"hljs-keyword\">in<\/span> value.items():\n        print(nested_key, <span class=\"hljs-string\">':'<\/span>, nested_value)\n    print(<span class=\"hljs-string\">'\\n'<\/span>)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code19', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code19');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Student 3 name: Kelly\nStudent 3 marks: 85\n\nClass details\n\nstudent1\nname: Jessa\nstate: Texas\ncity: Houston\nmarks: 75\n\nstudent2\nname: Emma\nstate: Texas\ncity: Dallas\nmarks: 60\n\nstudent3\nname: Kelly\nstate: Texas\ncity: Austin\nmarks : 85<\/pre>\n\n\n\n<p>As seen in the above example, we are adding three individual dictionaries inside a single dictionary.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-sort-dictionary\">Sort dictionary<\/h2>\n\n\n\n<p>The built-in method sorted() will sort the keys in the dictionary and returns a sorted list. In case we want to sort the values we can first get the values using the <code>values()<\/code> and then sort them.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code20\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">dict1 = {<span class=\"hljs-string\">'c'<\/span>: <span class=\"hljs-number\">45<\/span>, <span class=\"hljs-string\">'b'<\/span>: <span class=\"hljs-number\">95<\/span>, <span class=\"hljs-string\">'a'<\/span>: <span class=\"hljs-number\">35<\/span>}\n\n<span class=\"hljs-comment\"># sorting dictionary by keys<\/span>\nprint(sorted(dict1.items()))\n<span class=\"hljs-comment\"># Output &#91;('a', 35), ('b', 95), ('c', 45)]<\/span>\n\n<span class=\"hljs-comment\"># sort dict eys<\/span>\nprint(sorted(dict1))\n<span class=\"hljs-comment\"># output &#91;'a', 'b', 'c']<\/span>\n\n<span class=\"hljs-comment\"># sort dictionary values<\/span>\nprint(sorted(dict1.values()))\n<span class=\"hljs-comment\"># output &#91;35, 45, 95]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code20', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code20');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As we can see in the above example the keys are sorted in the first function call and in the second the values are sorted after the <code>values()<\/code> method call.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-dictionary-comprehension\">Dictionary comprehension<\/h2>\n\n\n\n<p>Dictionary comprehension is one way of creating the dictionary where the values of the key values are generated in a for-loop and we can filter the items to be added to the dictionary with an optional if condition. The general syntax is as follows<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">output_dictionary = {key : value <span class=\"hljs-keyword\">for<\/span> key,value <span class=\"hljs-keyword\">in<\/span> iterable &#91;<span class=\"hljs-keyword\">if<\/span> key,value condition1]}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Let us see this with a few examples.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code21\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># calculate the square of each even number from a list and store in dict<\/span>\nnumbers = &#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">8<\/span>]\neven_squares = {x: x ** <span class=\"hljs-number\">2<\/span> <span class=\"hljs-keyword\">for<\/span> x <span class=\"hljs-keyword\">in<\/span> numbers <span class=\"hljs-keyword\">if<\/span> x % <span class=\"hljs-number\">2<\/span> == <span class=\"hljs-number\">0<\/span>}\nprint(even_squares)\n\n<span class=\"hljs-comment\"># output {2: 4, 8: 64}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code21', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code21');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>Here in this example, we can see that a dictionary is created with an input list (any iterable can be given), the numbers from the list being the key and the value is the square of a number.<\/p>\n\n\n\n<p>We can even have two different iterables for the key and value and zip them inside the <code>for loop<\/code> to create a dictionary.<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">telephone_book = &#91;<span class=\"hljs-number\">1178<\/span>, <span class=\"hljs-number\">4020<\/span>, <span class=\"hljs-number\">5786<\/span>]\npersons = &#91;<span class=\"hljs-string\">'Jessa'<\/span>, <span class=\"hljs-string\">'Emma'<\/span>, <span class=\"hljs-string\">'Kelly'<\/span>]\n\ntelephone_Directory = {key: value <span class=\"hljs-keyword\">for<\/span> key, value <span class=\"hljs-keyword\">in<\/span> zip(persons, telephone_book)}\nprint(telephone_Directory)\n<span class=\"hljs-comment\"># Output {'Jessa': 1178, 'Emma': 4020, 'Kelly': 5786}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the above example, we are creating a telephone directory with separate tuples for the key which is the name, and the telephone number which is the value. We are zipping both the tuples together inside the for a loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-built-in-functions-with-dictionary\">Python Built-in functions with dictionary<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-max-and-min\">max() and min()<\/h3>\n\n\n\n<p>As the name suggests the <code>max() and min()<\/code> functions will return the keys with maximum and minimum values in a dictionary respectively. Only the keys are considered here not their corresponding values.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code22\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">dict = {<span class=\"hljs-number\">1<\/span>:<span class=\"hljs-string\">'aaa'<\/span>,<span class=\"hljs-number\">2<\/span>:<span class=\"hljs-string\">'bbb'<\/span>,<span class=\"hljs-number\">3<\/span>:<span class=\"hljs-string\">'AAA'<\/span>}\nprint(<span class=\"hljs-string\">'Maximum Key'<\/span>,max(dict)) <span class=\"hljs-comment\"># 3<\/span>\nprint(<span class=\"hljs-string\">'Minimum Key'<\/span>,min(dict)) <span class=\"hljs-comment\"># 1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code22', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code22');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As we can see <code>max() and min()<\/code> considered the keys to finding the maximum and minimum values respectively.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-all\">all()<\/h3>\n\n\n\n<p>When the built-in function <code>all()<\/code> is used with the dictionary the return value will be true in the case of all &#8211; true keys and false in case one of the keys is false.<\/p>\n\n\n\n<p>Few things to note here are<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Only key values should be true<\/li><li>The key values can be either True or 1 or &#8216;0&#8217;<\/li><li>0 and False in Key will return false<\/li><li>An empty dictionary will return true.<\/li><\/ul>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code23\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-25\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\">#dictionary with both 'true' keys<\/span>\ndict1 = {<span class=\"hljs-number\">1<\/span>:<span class=\"hljs-string\">'True'<\/span>,<span class=\"hljs-number\">1<\/span>:<span class=\"hljs-string\">'False'<\/span>}\n\n<span class=\"hljs-comment\">#dictionary with one false key<\/span>\ndict2 = {<span class=\"hljs-number\">0<\/span>:<span class=\"hljs-string\">'True'<\/span>,<span class=\"hljs-number\">1<\/span>:<span class=\"hljs-string\">'False'<\/span>}\n\n<span class=\"hljs-comment\">#empty dictionary<\/span>\ndict3= {}\n\n<span class=\"hljs-comment\">#'0' is true actually<\/span>\ndict4 = {<span class=\"hljs-string\">'0'<\/span>:<span class=\"hljs-literal\">False<\/span>}\n\nprint(<span class=\"hljs-string\">'All True Keys::'<\/span>,all(dict1))\nprint(<span class=\"hljs-string\">'One False Key'<\/span>,all(dict2))\nprint(<span class=\"hljs-string\">'Empty Dictionary'<\/span>,all(dict3))\nprint(<span class=\"hljs-string\">'With 0 in single quotes'<\/span>,all(dict4))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code23', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code23');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">All True Keys:: True\nOne False Key False\nEmpty Dictionary True\nWith 0 in single quotes True<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-any\">any()<\/h3>\n\n\n\n<p><code>any()<\/code> function will return true if dictionary keys contain anyone false which could be 0 or false. Let us see what <code>any()<\/code> method will return for the above cases.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code24\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-26\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\">#dictionary with both 'true' keys<\/span>\ndict1 = {<span class=\"hljs-number\">1<\/span>:<span class=\"hljs-string\">'True'<\/span>,<span class=\"hljs-number\">1<\/span>:<span class=\"hljs-string\">'False'<\/span>}\n\n<span class=\"hljs-comment\">#dictionary with one false key<\/span>\ndict2 = {<span class=\"hljs-number\">0<\/span>:<span class=\"hljs-string\">'True'<\/span>,<span class=\"hljs-number\">1<\/span>:<span class=\"hljs-string\">'False'<\/span>}\n\n<span class=\"hljs-comment\">#empty dictionary<\/span>\ndict3= {}\n\n<span class=\"hljs-comment\">#'0' is true actually<\/span>\ndict4 = {<span class=\"hljs-string\">'0'<\/span>:<span class=\"hljs-literal\">False<\/span>}\n\n<span class=\"hljs-comment\">#all false<\/span>\ndict5 = {<span class=\"hljs-number\">0<\/span>:<span class=\"hljs-literal\">False<\/span>}\n\nprint(<span class=\"hljs-string\">'All True Keys::'<\/span>,any(dict1))\nprint(<span class=\"hljs-string\">'One False Key ::'<\/span>,any(dict2))\nprint(<span class=\"hljs-string\">'Empty Dictionary ::'<\/span>,any(dict3))\nprint(<span class=\"hljs-string\">'With 0 in single quotes ::'<\/span>,any(dict4))\nprint(<span class=\"hljs-string\">'all false :: '<\/span>,any(dict5))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-26\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code24', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code24');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">All True Keys:: True\nOne False Key :: True\nEmpty Dictionary :: False\nWith 0 in single quotes :: True\nall false ::  False<\/pre>\n\n\n\n<p>As we can see this method returns true even if there is one true value and one thing to note here is that it <strong>returns false for empty dictionary<\/strong> and all false dictionary.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-when-to-use-dictionaries\">When to use dictionaries?<\/h3>\n\n\n\n<p>Dictionaries are items stored in Key-Value pairs that actually use the mapping format to actually store the values. It uses hashing internally for this. For retrieving a value with its key, the time taken will be very less as <code>O(1)<\/code>.<\/p>\n\n\n\n<p>For example, consider the phone lookup where it is very easy and fast to find the phone number (value) when we know the name (key) associated with it.<\/p>\n\n\n\n<p>So to associate values with keys in a more optimized format and to retrieve them efficiently using that key, later on, dictionaries could be used.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-summary-of-dictionary-operations\">Summary of dictionary operations<\/h2>\n\n\n\n<p>I want to hear from you. What do you think of this guide on Python dictionaries? Let me know by leaving a comment below.<\/p>\n\n\n\n<p>Also, try to solve the <a href=\"https:\/\/pynative.com\/python-dictionary-exercise-with-solutions\/\">Python dictionary exercise<\/a> and <a href=\"https:\/\/pynative.com\/python-dictionary-quiz\/\">dictionary Quiz<\/a>.<\/p>\n\n\n\n<p><strong>Below is the summary<\/strong> of all operations that we learned in this lesson<\/p>\n\n\n\n<p>Assume d1 and d2 are dictionaries with following items.<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-27\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">d1 = {<span class=\"hljs-string\">'a'<\/span>: <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-string\">'b'<\/span>: <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-string\">'c'<\/span>: <span class=\"hljs-number\">30<\/span>}\nd2 = {<span class=\"hljs-string\">'d'<\/span>: <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-string\">'e'<\/span>: <span class=\"hljs-number\">50<\/span>, <span class=\"hljs-string\">'f'<\/span>: <span class=\"hljs-number\">60<\/span>}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-27\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Operations<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>dict({'a': 10, 'b': 20})<\/code><\/td><td>Create a dictionary using a <code>dict()<\/code> constructor.<\/td><\/tr><tr><td><code>d2 = {}<\/code><\/td><td>Create an empty dictionary.<\/td><\/tr><tr><td><code>d1.get('a')<\/code><\/td><td>Retrieve value using the key name <code>a<\/code>.<\/td><\/tr><tr><td><code>d1.keys()<\/code><\/td><td>Returns a list of keys present in the dictionary.<\/td><\/tr><tr><td><code>d1.values()<\/code><\/td><td>Returns a list with all the values in the dictionary.<\/td><\/tr><tr><td><code>d1.items()<\/code><\/td><td>Returns a list of all the items in the dictionary with each key-value pair inside a tuple.<\/td><\/tr><tr><td><code>len(d1)<\/code><\/td><td>Returns number of items in a dictionary.<\/td><\/tr><tr><td><code>d1['d'] = 40<\/code><\/td><td>Update dictionary by adding a new key.<\/td><\/tr><tr><td><code>d1.update({'e': 50, 'f': 60})<\/code><\/td><td>Add multiple keys to the dictionary.<\/td><\/tr><tr><td><code>d1.setdefault('g', 70)<\/code><\/td><td>Set the default value if a key doesn&#8217;t exist.<\/td><\/tr><tr><td><code>d1['b'] = 100<\/code><\/td><td>Modify the values of the existing key.<\/td><\/tr><tr><td><code>d1.pop('b')<\/code><\/td><td>Remove the key <code>b<\/code> from the dictionary.<\/td><\/tr><tr><td><code>d1.popitem()<\/code><\/td><td>Remove any random item from a dictionary.<\/td><\/tr><tr><td><code>d1.clear()<\/code><\/td><td>Removes all items from the dictionary.<\/td><\/tr><tr><td><code>'key' in d1.keys()<\/code><\/td><td>Check if a <code>key<\/code> exists in a dictionary.<\/td><\/tr><tr><td><code>d1.update(d2)<\/code><\/td><td>Add all items of dictionary <code>d2<\/code> into <code>d1<\/code>.<\/td><\/tr><tr><td><code>d3= {**d1, **d2}<\/code><\/td><td>Join two dictionaries.<\/td><\/tr><tr><td><code>d2 = d1.copy()<\/code><\/td><td>Copy dictionary <code>d1<\/code> into <code>d2<\/code>.<\/td><\/tr><tr><td><code>max(d1)<\/code><\/td><td>Returns the key with the maximum value in the dictionary <code>d1<\/code><\/td><\/tr><tr><td><code>min(d1)<\/code><\/td><td>Returns the key with the minimum value in the dictionary <code>d1<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Dictionaries are ordered collections of unique values stored in (Key-Value) pairs. In Python version 3.7 and onwards, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered. Python dictionary represents a mapping between a key and a value. In simple terms, a Python dictionary can store pairs of keys and values. Each key is [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":7992,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[22,1],"tags":[32],"class_list":{"0":"post-7732","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python","8":"category-basics","9":"tag-python-basics","10":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Dictionaries in Python &#8211; PYnative<\/title>\n<meta name=\"description\" content=\"Python Dictionaries are unordered collections of unique values stored in (Key-Value) pairs. Learn how to access and manage dictionary data.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/pynative.com\/python-dictionaries\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Dictionaries in Python\" \/>\n<meta property=\"og:description\" content=\"Python Dictionaries are unordered collections of unique values stored in (Key-Value) pairs. Learn how to access and manage dictionary data.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/pynative.com\/python-dictionaries\/\" \/>\n<meta property=\"og:site_name\" content=\"PYnative\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-12T17:44:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-03T11:52:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/dictionaries-in-python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"679\" \/>\n\t<meta property=\"og:image:height\" content=\"416\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Vishal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@PyNative\" \/>\n<meta name=\"twitter:site\" content=\"@PyNative\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"18 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-dictionaries\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-dictionaries\\\/\"},\"author\":{\"name\":\"Vishal\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"headline\":\"Dictionaries in Python\",\"datePublished\":\"2021-02-12T17:44:37+00:00\",\"dateModified\":\"2022-11-03T11:52:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-dictionaries\\\/\"},\"wordCount\":2425,\"commentCount\":12,\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"image\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-dictionaries\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/dictionaries-in-python.jpg\",\"keywords\":[\"Python Basics\"],\"articleSection\":[\"Python\",\"Python Basics\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-dictionaries\\\/#respond\"]}],\"accessibilityFeature\":[\"tableOfContents\"]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-dictionaries\\\/\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-dictionaries\\\/\",\"name\":\"Dictionaries in Python &#8211; PYnative\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-dictionaries\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-dictionaries\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/dictionaries-in-python.jpg\",\"datePublished\":\"2021-02-12T17:44:37+00:00\",\"dateModified\":\"2022-11-03T11:52:50+00:00\",\"description\":\"Python Dictionaries are unordered collections of unique values stored in (Key-Value) pairs. Learn how to access and manage dictionary data.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-dictionaries\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-dictionaries\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-dictionaries\\\/#primaryimage\",\"url\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/dictionaries-in-python.jpg\",\"contentUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/dictionaries-in-python.jpg\",\"width\":679,\"height\":416,\"caption\":\"Dictionaries in Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-dictionaries\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/pynative.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\\\/\\\/pynative.com\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Dictionaries in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\",\"url\":\"https:\\\/\\\/pynative.com\\\/\",\"name\":\"PYnative\",\"description\":\"Python Programming\",\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/pynative.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\",\"name\":\"Vishal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"url\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"contentUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"width\":968,\"height\":1065,\"caption\":\"Vishal\"},\"logo\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\"},\"description\":\"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!\",\"sameAs\":[\"https:\\\/\\\/pynative.com\"]}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Dictionaries in Python &#8211; PYnative","description":"Python Dictionaries are unordered collections of unique values stored in (Key-Value) pairs. Learn how to access and manage dictionary data.","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:\/\/pynative.com\/python-dictionaries\/","og_locale":"en_US","og_type":"article","og_title":"Dictionaries in Python","og_description":"Python Dictionaries are unordered collections of unique values stored in (Key-Value) pairs. Learn how to access and manage dictionary data.","og_url":"https:\/\/pynative.com\/python-dictionaries\/","og_site_name":"PYnative","article_published_time":"2021-02-12T17:44:37+00:00","article_modified_time":"2022-11-03T11:52:50+00:00","og_image":[{"width":679,"height":416,"url":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/dictionaries-in-python.jpg","type":"image\/jpeg"}],"author":"Vishal","twitter_card":"summary_large_image","twitter_creator":"@PyNative","twitter_site":"@PyNative","twitter_misc":{"Written by":"Vishal","Est. reading time":"18 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/pynative.com\/python-dictionaries\/#article","isPartOf":{"@id":"https:\/\/pynative.com\/python-dictionaries\/"},"author":{"name":"Vishal","@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"headline":"Dictionaries in Python","datePublished":"2021-02-12T17:44:37+00:00","dateModified":"2022-11-03T11:52:50+00:00","mainEntityOfPage":{"@id":"https:\/\/pynative.com\/python-dictionaries\/"},"wordCount":2425,"commentCount":12,"publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"image":{"@id":"https:\/\/pynative.com\/python-dictionaries\/#primaryimage"},"thumbnailUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/dictionaries-in-python.jpg","keywords":["Python Basics"],"articleSection":["Python","Python Basics"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/pynative.com\/python-dictionaries\/#respond"]}],"accessibilityFeature":["tableOfContents"]},{"@type":"WebPage","@id":"https:\/\/pynative.com\/python-dictionaries\/","url":"https:\/\/pynative.com\/python-dictionaries\/","name":"Dictionaries in Python &#8211; PYnative","isPartOf":{"@id":"https:\/\/pynative.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/pynative.com\/python-dictionaries\/#primaryimage"},"image":{"@id":"https:\/\/pynative.com\/python-dictionaries\/#primaryimage"},"thumbnailUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/dictionaries-in-python.jpg","datePublished":"2021-02-12T17:44:37+00:00","dateModified":"2022-11-03T11:52:50+00:00","description":"Python Dictionaries are unordered collections of unique values stored in (Key-Value) pairs. Learn how to access and manage dictionary data.","breadcrumb":{"@id":"https:\/\/pynative.com\/python-dictionaries\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/pynative.com\/python-dictionaries\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pynative.com\/python-dictionaries\/#primaryimage","url":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/dictionaries-in-python.jpg","contentUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/dictionaries-in-python.jpg","width":679,"height":416,"caption":"Dictionaries in Python"},{"@type":"BreadcrumbList","@id":"https:\/\/pynative.com\/python-dictionaries\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/pynative.com\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/pynative.com\/python\/"},{"@type":"ListItem","position":3,"name":"Dictionaries in Python"}]},{"@type":"WebSite","@id":"https:\/\/pynative.com\/#website","url":"https:\/\/pynative.com\/","name":"PYnative","description":"Python Programming","publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/pynative.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f","name":"Vishal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","url":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","contentUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","width":968,"height":1065,"caption":"Vishal"},"logo":{"@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg"},"description":"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!","sameAs":["https:\/\/pynative.com"]}]}},"featured_image_src":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/dictionaries-in-python.jpg","featured_image_src_square":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/dictionaries-in-python.jpg","author_info":{"display_name":"Vishal","author_link":"https:\/\/pynative.com\/author\/vishal\/"},"_links":{"self":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/7732","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/comments?post=7732"}],"version-history":[{"count":0,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/7732\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/media\/7992"}],"wp:attachment":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/media?parent=7732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/categories?post=7732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/tags?post=7732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}