{"id":10361,"date":"2016-01-25T16:15:16","date_gmt":"2016-01-25T14:15:16","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=10361"},"modified":"2018-01-09T09:48:21","modified_gmt":"2018-01-09T07:48:21","slug":"python-dictionary-example","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/","title":{"rendered":"Python Dictionary Example"},"content":{"rendered":"<p>Dictionaries in Python are data structures that optimize element lookups by associating keys to values. You could say that dictionaries are arrays of (key, value) pairs.<\/p>\n<h2>1. Define<\/h2>\n<p>Their syntax is pretty familiar to us, web developers, since its exactly the same as JSON&#8217;s. An object starts and ends in curly braces, each pair is separated by a comma, and the (key, value) pairs are associated through a colon (:). Let&#8217;s see:<\/p>\n<p><span style=\"text-decoration: underline\"><em>base_example.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\nEXAMPLE_DICT = {\r\n    \"animals\": [\"dog\", \"cat\", \"fish\"],\r\n    \"a_number\": 1,\r\n    \"a_name\": \"Sebasti\u00e1n\",\r\n    \"a_boolean\": True,\r\n    \"another_dict\": {\r\n        \"you could\": \"keep going\",\r\n        \"like this\": \"forever\"\r\n    }\r\n}\r\n<\/pre>\n<p>Here, we are defining a dictionary which contains an array of animals, a number, a name, a boolean and another dictionary inside. This data structure is pretty versatile, as you can see. By the way, to define an empty dictionary you just write <code>my_dict = {}<\/code>.<br \/>\n&nbsp;<br \/>\n[ulp id=&#8217;R7QVpFMZmjosABLC&#8217;]<\/p>\n<h2>2. Read<\/h2>\n<p>Now, let&#8217;s see how to retrieve a value from this dictionary. There are two ways of doing this: The known &#8220;[]&#8221; syntax where we write <code>dict[\"key\"]<\/code> and get the result, or the <code>dict.get(\"key\", \"default value\")<\/code> which also returns the result.<\/p>\n<p>What&#8217;s the difference? Well, the <em>[]<\/em> syntax results in a KeyError if the key is not defined, and we usually don&#8217;t want that. The <em>get<\/em> method receives the key and an optional default value, and it does not result in any annoying error if the key is not found, it just returns None.<\/p>\n<p>Let&#8217;s check out the <em>[]<\/em> syntax a little bit:<\/p>\n<p><span style=\"text-decoration: underline\"><em>base_example.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\nEXAMPLE_DICT = {\r\n    \"animals\": [\"dog\", \"cat\", \"fish\"],\r\n    \"a_number\": 1,\r\n    \"a_name\": \"Sebasti\u00e1n\",\r\n    \"a_boolean\": True,\r\n    \"another_dict\": {\r\n        \"you could\": \"keep going\",\r\n        \"like this\": \"forever\"\r\n    }\r\n}\r\n\r\nprint(str(EXAMPLE_DICT[\"animals\"]))  # outputs ['dog', 'cat', 'fish']\r\nprint(str(EXAMPLE_DICT[\"a_number\"]))  # outputs 1\r\nprint(str(EXAMPLE_DICT[\"this_one_does_not_exist\"]))  # throws KeyError\r\n\r\n<\/pre>\n<p>In every print statement you will find a comment predicting the result, anyway, let&#8217;s see the actual output:<\/p>\n<pre class=\"brush:bash\">\r\n['dog', 'cat', 'fish']\r\nTraceback (most recent call last):\r\n1\r\n  File \"\/home\/svinci\/projects\/jcg\/dictionaries\/base_example.py\", line 14, in &lt;module&gt;\r\n    print(str(EXAMPLE_DICT[\"this_one_does_not_exist\"]))\r\nKeyError: 'this_one_does_not_exist'\r\n<\/pre>\n<p>There it is, the key <em>&#8220;this_one_does_not_exist&#8221;<\/em> does not exist, so a <em>KeyError<\/em> is raised. Let&#8217;s put that KeyError in an except clause, and check the <em>get<\/em> method.<\/p>\n<p><span style=\"text-decoration: underline\"><em>base_example.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\nEXAMPLE_DICT = {\r\n    \"animals\": [\"dog\", \"cat\", \"fish\"],\r\n    \"a_number\": 1,\r\n    \"a_name\": \"Sebasti\u00e1n\",\r\n    \"a_boolean\": True,\r\n    \"another_dict\": {\r\n        \"you could\": \"keep going\",\r\n        \"like this\": \"forever\"\r\n    }\r\n}\r\n\r\ndefault_message = \"oops, key not found\"\r\n\r\nprint(str(EXAMPLE_DICT[\"animals\"]))  # outputs ['dog', 'cat', 'fish']\r\nprint(str(EXAMPLE_DICT[\"a_number\"]))  # outputs 1\r\n\r\ntry:\r\n    print(str(EXAMPLE_DICT[\"this_one_does_not_exist\"]))  # throws KeyError\r\nexcept KeyError:\r\n    print(\"KeyError\")\r\n\r\nprint(str(EXAMPLE_DICT.get(\"animals\", default_message)))  # outputs ['dog', 'cat', 'fish']\r\nprint(str(EXAMPLE_DICT.get(\"a_number\", default_message)))  # outputs 1\r\nprint(str(EXAMPLE_DICT.get(\"this_one_does_not_exist\")))  # outputs None\r\nprint(str(EXAMPLE_DICT.get(\"this_one_does_not_exist\", default_message)))  # outputs \"oops, key not found\"\r\n\r\n<\/pre>\n<p>Here, we put the missing key in a try\/except, which prints <em>&#8220;KeyError&#8221;<\/em> on failure. Then we see the <em>get<\/em> method examples, we ask for a key and return a default message if it&#8217;s not found. The output:<\/p>\n<pre class=\"brush:bash\">\r\n['dog', 'cat', 'fish']\r\n1\r\nKeyError\r\n['dog', 'cat', 'fish']\r\n1\r\nNone\r\noops, key not found\r\n<\/pre>\n<p>It&#8217;s pretty much the same behavior, but anything that doesn&#8217;t include a try\/except is nicer.<\/p>\n<h2>3. Write<\/h2>\n<p>Dictionaries are mutable data structures. We can add keys to it, update their values and even delete them. Let&#8217;s see one by one in our example:<\/p>\n<p><span style=\"text-decoration: underline\"><em>base_example.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\nEXAMPLE_DICT = {\r\n    \"animals\": [\"dog\", \"cat\", \"fish\"],\r\n    \"a_number\": 1,\r\n    \"a_name\": \"Sebasti\u00e1n\",\r\n    \"a_boolean\": True,\r\n    \"another_dict\": {\r\n        \"you could\": \"keep going\",\r\n        \"like this\": \"forever\"\r\n    }\r\n}\r\n\r\n\r\ndef print_key(dictionary, key):\r\n    print(str(dictionary.get(key, \"Key was not found\")))\r\n\r\n# Create a key\r\nEXAMPLE_DICT[\"this_one_does_not_exist\"] = \"it exists now\"  # This statement will create the key \"this_one_does_not_exist\" and assign to it the value \"it exists now\"\r\nprint_key(EXAMPLE_DICT, \"this_one_does_not_exist\")\r\n\r\n# Update a key\r\nEXAMPLE_DICT[\"a_boolean\"] = False  # Exactly, it looks the same, and behaves the same. It just overwrites the given key.\r\nprint_key(EXAMPLE_DICT, \"a_boolean\")\r\n\r\n# Delete a key\r\ndel EXAMPLE_DICT[\"this_one_does_not_exist\"]  # Now \"this_one_does_not_exist\" ceased from existing (Again).\r\nprint_key(EXAMPLE_DICT, \"this_one_does_not_exist\")\r\n<\/pre>\n<p>There is a method called <code>print_key<\/code> which receives a dictionary and a key. It prints the value of the given key in the given dictionary or <em>&#8220;Key was not found&#8221;<\/em> instead.<\/p>\n<p>We are creating the key <em>this_one_does_not_exist<\/em> first, and printing it out. Then we are updating <em>a_boolean<\/em> and also printing it. At last, we delete <em>this_one_does_not_exist<\/em> from the dict and print it again. The result:<\/p>\n<pre class=\"brush:bash\">\r\nit exists now\r\nFalse\r\nKey was not found\r\n<\/pre>\n<p>It works as expected. Notice that, in the context of writing a key, the <em>[]<\/em> syntax doesn&#8217;t throw an error if the key was not found, so in this case you can use it without worrying.<\/p>\n<h2>4. Useful operations<\/h2>\n<p>Python provides some useful operations to work with dictionaries, let&#8217;s see some of them:<\/p>\n<h3>4.1. In (keyword)<\/h3>\n<p>The <em>in<\/em> keyword is a tool Python provides to, in this case, check whether a key is present in a dictionary or not. Let&#8217;s make our own implementation of the <em>get<\/em> method to test this keyword:<\/p>\n<p><span style=\"text-decoration: underline\"><em>dict_get.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\ndef get(dictionary, key, default_value=None):\r\n    if key in dictionary:\r\n        return dictionary[key]\r\n    else:\r\n        return default_value\r\n\r\n\r\nmy_dict = {\r\n    \"name\": \"Sebasti\u00e1n\",\r\n    \"age\": 21\r\n}\r\n\r\nprint(str(get(my_dict, \"name\", \"Name was not present\")))\r\nprint(str(get(my_dict, \"age\", \"Age was not present\")))\r\nprint(str(get(my_dict, \"address\", \"Address was not present\")))\r\n\r\n<\/pre>\n<p>Withing our custom <em>get<\/em> method, we use the <em>[]<\/em> syntax to retrieve the requested key from the given dictionary, but not without checking if the key is present there with the <em>in<\/em> keyword. We receive a default value which by default is <em>None<\/em>. It behaves exactly like the native <em>get<\/em> method, here is the output:<\/p>\n<pre class=\"brush:bash\">\r\nSebasti\u00e1n\r\n21\r\nAddress was not present\r\n<\/pre>\n<p>This tool can also be used to iterate over the dictionary keys, let&#8217;s implement a method which returns an array with all the keys in a dictionary to see it working:<\/p>\n<p><span style=\"text-decoration: underline\"><em>dict_all_keys.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\nmy_dict = {\r\n    \"name\": \"Sebasti\u00e1n\",\r\n    \"age\": 21\r\n}\r\n\r\n\r\ndef keys(dictionary):\r\n    return [k for k in dictionary]\r\n\r\n\r\nprint(str(keys(my_dict)))\r\n<\/pre>\n<p>It&#8217;s pretty simple, right? In human language, that iterates for every key <em>in<\/em> the given dictionary and returns an array with them. The output:<\/p>\n<pre class=\"brush:bash\">\r\n['name', 'age']\r\n<\/pre>\n<h3>4.2. Len (built-in function)<\/h3>\n<p>The <code>len<\/code> function returns the number of key-value pairs in a dictionary. It&#8217;s data types do not matter in this context. Notice the fact that it returns the number of <strong>key-value pairs<\/strong>, so a dictionary that looks like <code>{\"key\", \"value\"}<\/code> will return <em>1<\/em> when asked for its length.<\/p>\n<p>Its use is pretty simple, it receives the dictionary in question as argument, and just for testing purposes we&#8217;ll modify the <em>dict_all_keys.py<\/em> to check that the number of keys its returning is the same as the len of the dict.<\/p>\n<p><span style=\"text-decoration: underline\"><em>dict_all_keys.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\nmy_dict = {\r\n    \"name\": \"Sebasti\u00e1n\",\r\n    \"age\": 21\r\n}\r\n\r\n\r\ndef keys(dictionary):\r\n    result = [k for k in dictionary]\r\n    # result.append(\"break the program plz\")\r\n    if len(result) != len(dictionary):\r\n        raise Exception('expected {} keys. got {}'.format(len(dictionary), len(result)))\r\n    return result\r\n\r\n\r\nprint(str(keys(my_dict)))\r\n\r\n<\/pre>\n<p>There is the check, if we uncomment the second line of the function&#8217;s body we&#8217;ll see the exception raised:<\/p>\n<pre class=\"brush:bash\">\r\nTraceback (most recent call last):\r\n  File \"\/home\/svinci\/projects\/jcg\/dictionaries\/dict_all_keys.py\", line 15, in &lt;module&lt;\r\n    print(str(keys(my_dict)))\r\n  File \"\/home\/svinci\/projects\/jcg\/dictionaries\/dict_all_keys.py\", line 11, in keys\r\n    raise Exception('expected {} keys. got {}'.format(len(dictionary), len(result)))\r\nException: expected 2 keys. got 3\r\n<\/pre>\n<p>The message <em><strong>Exception: expected 2 keys. got 3<\/strong><\/em> is telling us that the keys we are about to return contain an extra key. This is another useless but explicit example.<\/p>\n<h3>4.3. keys() and values()<\/h3>\n<p>Let&#8217;s see a couple functions that render obsolete the function <em>keys<\/em> we just wrote. These are <code>keys()<\/code> and <code>values()<\/code>. I think it&#8217;s pretty intuitive what these functions do, <strong>keys<\/strong> returns every key in the dictionary and <strong>values<\/strong> returns every value in the dictionary.<\/p>\n<p>Having a dictionary like <code>me = {\"name\": \"Sebasti\u00e1n\", \"age\": 21}<\/code>, <code>me.keys()<\/code> will return <code>[\"name\", \"age\"]<\/code> and <code>me.values()<\/code> will return <code>[\"Sebasti\u00e1n\", 21]<\/code>. Kind of&#8230; see, dictionaries in python are not ordered, then, the result of <code>keys<\/code> and <code>values<\/code> aren&#8217;t either. If you need to sort either keys or values just call <code>sorted<\/code> passing the array as argument.<\/p>\n<p>Let&#8217;s see an example:<\/p>\n<p><span style=\"text-decoration: underline\"><em>dict_keys_values.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\ndef print_dict(dictionary):\r\n    ks = list(dictionary.keys())\r\n    vs = list(dictionary.values())\r\n    for i in range(0, len(ks)):\r\n        k = ks[i]\r\n        v = vs[i]\r\n        print(\"{}: {}\".format(str(k), str(v)))\r\n\r\n\r\nexample = {\r\n    \"name\": \"Sebasti\u00e1n\",\r\n    \"last_name\": \"Vinci\",\r\n    \"age\": 21\r\n}\r\n\r\nprint_dict(example)\r\n\r\n<\/pre>\n<p>Here we are creating a dictionary containing some keys, and then iterating over the keys and values to print them out. I know it&#8217;s not the prettiest code but it proves my point. When we execute it one time we see:<\/p>\n<pre class=\"brush:bash\">\r\nage: 21\r\nname: Sebasti\u00e1n\r\nlast_name: Vinci\r\n<\/pre>\n<p>Then a second time:<\/p>\n<pre class=\"brush:bash\">\r\nlast_name: Vinci\r\nage: 21\r\nname: Sebasti\u00e1n\r\n<\/pre>\n<p>See? That&#8217;s because dictionaries&#8217; keys are not sorted, to solve this issue (and fix that awful piece of code) let&#8217;s change that script a little bit. We won&#8217;t be using the function <code>values<\/code> anymore since it&#8217;s not necessary, but I think it&#8217;s pretty clear the way to use it.<\/p>\n<p><span style=\"text-decoration: underline\"><em>dict_keys_values.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\ndef print_dict(dictionary):\r\n    ks = sorted(dictionary.keys())\r\n    for k in ks:\r\n        print(\"{}: {}\".format(str(k), str(dictionary.get(k))))\r\n\r\n\r\nexample = {\r\n    \"name\": \"Sebasti\u00e1n\",\r\n    \"last_name\": \"Vinci\",\r\n    \"age\": 21\r\n}\r\n\r\nprint_dict(example)\r\n<\/pre>\n<p>Now we&#8217;ll always see the same output, as the keys are being sorted alphabetically. The output:<\/p>\n<pre class=\"brush:bash\">\r\nage: 21\r\nlast_name: Vinci\r\nname: Sebasti\u00e1n\r\n<\/pre>\n<p>Now, why am I giving so much importance to the lack of sorting of dictionaries? Well, imagine you are storing it in a CSV file. Every time you write a dictionary, as the keys are in arbitrary orders, the CSV&#8217;s rows won&#8217;t be consistent, and it will mess up your program by writing each row with the columns in different orders. So when you need your data in a strict order, please keep in mind this little fact.<\/p>\n<h3>4.4. items()<\/h3>\n<p>This method returns a list of tuples containing every key-value pair in the dictionary as <code>({\"a\": 1, \"b\": 2}.items() =&gt; [(\"a\", 1), (\"b\", 2)])<\/code>. With this method we can iterate over this pair array more seamlessly (keep in mind that this is still unordered), so if we just want to print our dictionary and the order doesn&#8217;t matter we can write:<\/p>\n<p><span style=\"text-decoration: underline\"><em>dict_items.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\ndef print_dict(dictionary):\r\n    for k, v in dictionary.items():\r\n        print(\"{}: {}\".format(str(k), str(v)))\r\n\r\n\r\nexample = {\r\n    \"name\": \"Sebasti\u00e1n\",\r\n    \"last_name\": \"Vinci\",\r\n    \"age\": 21\r\n}\r\n\r\nprint_dict(example)\r\n\r\n<\/pre>\n<p>This will output the same as the first <em>dict_keys_values.py<\/em>, <em>&#8220;key: value&#8221;<\/em> with different orders in each execution. Something to notice is the fact that we can not assign new values to tuples. We access values in tuples by <code>tuple[0]<\/code> and <code>tuple[1]<\/code>, but we can&#8217;t do <code>tuepl[0] = 1<\/code>. This will rase a <em>TypeError: &#8216;tuple&#8217; object does not support item assignment<\/em>.<\/p>\n<h3>4.5. update()<\/h3>\n<p>This method changes one dictionary to have new values from a second one. It modifies existing values, as dictionaries are mutable (keep this in mind). Let&#8217;s see an example:<\/p>\n<p><span style=\"text-decoration: underline\"><em>dict_update.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\nproduct = {\r\n    \"description\": \"WCG E-Book\",\r\n    \"price\": 2.75,\r\n    \"sold\": 1500,\r\n    \"stock\": 5700\r\n}\r\n\r\n\r\ndef sell(p):\r\n    update = {\"sold\": p.get(\"sold\", 0) + 1, \"stock\": p.get(\"stock\") - 1}\r\n    p.update(update)\r\n\r\n\r\ndef print_product(p):\r\n    print(\", \".join([\"{}: {}\".format(str(k), str(product[k])) for k in sorted(p.keys())]))\r\n\r\nprint_product(product)\r\nfor i in range(0, 100):\r\n    sell(product)\r\nprint_product(product)\r\n\r\n<\/pre>\n<p>Here we are printing the product, executing <code>sell<\/code> a hundred times and then printing the product again. The sell function updates de product subtracting one from <em>&#8220;stock&#8221;<\/em> and adding one to <em>&#8220;sold&#8221;<\/em>. The output:<\/p>\n<pre class=\"brush:bash\">\r\ndescription: WCG E-Book, price: 2.75, sold: 1500, stock: 5700\r\ndescription: WCG E-Book, price: 2.75, sold: 1600, stock: 5600\r\n<\/pre>\n<p>Notice that we don&#8217;t return a new dictionary with the updated values, it updates the existing one. This is an easy way of updating a couple keys in a dictionary without having to overwrite them one by one.<\/p>\n<h3>4.6. copy()<\/h3>\n<p>A dictionary&#8217;s copy method will copy the entire dictionary into a new one. One thing to notice is that it&#8217;s a shallow copy. If you have nested dictionaries, it will copy the first level, but the inner dictionaries will be a reference to the same object. Let&#8217;s see an example:<\/p>\n<p><span style=\"text-decoration: underline\"><em>dict_copy.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">\r\nme = {\r\n    \"name\": {\r\n        \"first\": \"Sebasti\u00e1n\",\r\n        \"last\": \"Vinci\"\r\n    },\r\n    \"age\": 21\r\n}\r\nmy_clone = me.copy()\r\n\r\nmy_clone[\"age\"] = 22\r\nprint(\"my age: {}, my clone's age: {}\".format(me.get(\"age\"), my_clone.get(\"age\")))\r\n\r\nmy_clone.get(\"name\")[\"first\"] = \"Michael\"\r\nprint(\"my name: {}, my clone's name: {}\".format(me.get(\"name\").get(\"first\"), my_clone.get(\"name\").get(\"first\")))\r\n<\/pre>\n<p>In this script, we are creating a dictionary with some data and then copying it. We change the clone&#8217;s age and print both, and then do the same with the first names.<\/p>\n<p>Now, my clone is a copy of me, it is not a reference to the same memory address, so if I change its age, mine will remain the same. But, the inner dictionary, the one containing the name, is a reference to the same memory address, when I change my clone&#8217;s name, mine will change too. Let&#8217;s see the output:<\/p>\n<pre class=\"brush:bash\">\r\nmy age: 21, my clone's age: 22\r\nmy name: Michael, my clone's name: Michael\r\n<\/pre>\n<p>So, having this in mind, we can use the <code>copy<\/code>, which, if used carefully, is a powerful and useful tool.<\/p>\n<h2>5. Download the Code Project<\/h2>\n<p>This was an basic example on Python Dictionaries.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/> You can download the full source code of this example here: <a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/01\/dictionaries.zip\"><strong>dictionaries<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Dictionaries in Python are data structures that optimize element lookups by associating keys to values. You could say that dictionaries are arrays of (key, value) pairs. 1. Define Their syntax is pretty familiar to us, web developers, since its exactly the same as JSON&#8217;s. An object starts and ends in curly braces, each pair is &hellip;<\/p>\n","protected":false},"author":109,"featured_media":1651,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[319,318,290],"class_list":["post-10361","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-dictionaries","tag-dictionary","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Dictionary Example - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"Dictionaries in Python are data structures that optimize element lookups by associating keys to values. You could say that dictionaries are arrays of\" \/>\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.webcodegeeks.com\/python\/python-dictionary-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Dictionary Example - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"Dictionaries in Python are data structures that optimize element lookups by associating keys to values. You could say that dictionaries are arrays of\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/sgvinci\" \/>\n<meta property=\"article:published_time\" content=\"2016-01-25T14:15:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-09T07:48:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sebastian Vinci\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@sebastianvinci_\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sebastian Vinci\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/\"},\"author\":{\"name\":\"Sebastian Vinci\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/06a43c63e373dff2e159bbc029b405aa\"},\"headline\":\"Python Dictionary Example\",\"datePublished\":\"2016-01-25T14:15:16+00:00\",\"dateModified\":\"2018-01-09T07:48:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/\"},\"wordCount\":1514,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"keywords\":[\"dictionaries\",\"dictionary\",\"python\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/\",\"name\":\"Python Dictionary Example - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"datePublished\":\"2016-01-25T14:15:16+00:00\",\"dateModified\":\"2018-01-09T07:48:21+00:00\",\"description\":\"Dictionaries in Python are data structures that optimize element lookups by associating keys to values. You could say that dictionaries are arrays of\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Dictionary Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/06a43c63e373dff2e159bbc029b405aa\",\"name\":\"Sebastian Vinci\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/12d0233b49dd2a282330a987b16e81c3fbd4a8a8f5d5338348a6edd47cfff99e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/12d0233b49dd2a282330a987b16e81c3fbd4a8a8f5d5338348a6edd47cfff99e?s=96&d=mm&r=g\",\"caption\":\"Sebastian Vinci\"},\"description\":\"Sebastian is a full stack programmer, who has strong experience in Java and Scala enterprise web applications. He is currently studying Computers Science in UBA (University of Buenos Aires) and working a full time job at a .com company as a Semi-Senior developer, involving architectural design, implementation and monitoring. He also worked in automating processes (such as data base backups, building, deploying and monitoring applications).\",\"sameAs\":[\"http:\/\/www.webcodegeeks.com\/\",\"https:\/\/www.facebook.com\/sgvinci\",\"https:\/\/x.com\/sebastianvinci_\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/sebastian-vinci\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Dictionary Example - Web Code Geeks - 2026","description":"Dictionaries in Python are data structures that optimize element lookups by associating keys to values. You could say that dictionaries are arrays of","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.webcodegeeks.com\/python\/python-dictionary-example\/","og_locale":"en_US","og_type":"article","og_title":"Python Dictionary Example - Web Code Geeks - 2026","og_description":"Dictionaries in Python are data structures that optimize element lookups by associating keys to values. You could say that dictionaries are arrays of","og_url":"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_author":"https:\/\/www.facebook.com\/sgvinci","article_published_time":"2016-01-25T14:15:16+00:00","article_modified_time":"2018-01-09T07:48:21+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","type":"image\/jpeg"}],"author":"Sebastian Vinci","twitter_card":"summary_large_image","twitter_creator":"@sebastianvinci_","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Sebastian Vinci","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/"},"author":{"name":"Sebastian Vinci","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/06a43c63e373dff2e159bbc029b405aa"},"headline":"Python Dictionary Example","datePublished":"2016-01-25T14:15:16+00:00","dateModified":"2018-01-09T07:48:21+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/"},"wordCount":1514,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","keywords":["dictionaries","dictionary","python"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/","url":"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/","name":"Python Dictionary Example - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","datePublished":"2016-01-25T14:15:16+00:00","dateModified":"2018-01-09T07:48:21+00:00","description":"Dictionaries in Python are data structures that optimize element lookups by associating keys to values. You could say that dictionaries are arrays of","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/python\/python-dictionary-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/www.webcodegeeks.com\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Dictionary Example"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/06a43c63e373dff2e159bbc029b405aa","name":"Sebastian Vinci","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/12d0233b49dd2a282330a987b16e81c3fbd4a8a8f5d5338348a6edd47cfff99e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/12d0233b49dd2a282330a987b16e81c3fbd4a8a8f5d5338348a6edd47cfff99e?s=96&d=mm&r=g","caption":"Sebastian Vinci"},"description":"Sebastian is a full stack programmer, who has strong experience in Java and Scala enterprise web applications. He is currently studying Computers Science in UBA (University of Buenos Aires) and working a full time job at a .com company as a Semi-Senior developer, involving architectural design, implementation and monitoring. He also worked in automating processes (such as data base backups, building, deploying and monitoring applications).","sameAs":["http:\/\/www.webcodegeeks.com\/","https:\/\/www.facebook.com\/sgvinci","https:\/\/x.com\/sebastianvinci_"],"url":"https:\/\/www.webcodegeeks.com\/author\/sebastian-vinci\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/10361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/109"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=10361"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/10361\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/1651"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=10361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=10361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=10361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}