{"id":3329,"date":"2020-06-23T17:06:55","date_gmt":"2020-06-23T11:36:55","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=3329"},"modified":"2026-07-13T12:26:56","modified_gmt":"2026-07-13T06:56:56","slug":"python-hashmaps","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-hashmaps\/","title":{"rendered":"Python Hashmaps: Dictionaries, Hashing, and O(1) Lookup"},"content":{"rendered":"<p><strong>Quick answer:<\/strong> A Python dictionary is the standard hashmap-like data structure. It maps hashable keys to values, gives average-case constant-time lookup for ordinary workloads, preserves insertion order, and handles collision details internally.<\/p>\n<figure class=\"pythonpool-article-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-hashmaps-2.png\" alt=\"Python Pool infographic explaining dictionaries, hashable keys, average lookup, collisions, and updates\" width=\"1536\" height=\"1024\" loading=\"lazy\" decoding=\"async\"><figcaption>Python dictionaries provide hashmap-style key lookup; choose hashable keys, understand average-case performance, and keep the key policy explicit.<\/figcaption><\/figure>\n<p>In Python, the everyday hashmap is the <code>dict<\/code>. A dictionary stores key-value pairs so code can look up a value by key without scanning every item manually. This makes dictionaries one of the most useful data structures in Python programs.<\/p>\n<p>The official Python documentation for <a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#mapping-types-dict\">dictionary mapping types<\/a>, the <a href=\"https:\/\/docs.python.org\/3\/tutorial\/datastructures.html#dictionaries\">dictionary tutorial<\/a>, and <a href=\"https:\/\/docs.python.org\/3\/glossary.html#term-hashable\">hashable objects<\/a> explains the core behavior.<\/p>\n<p>A hashmap uses a hash of the key to find where data belongs internally. Python handles those internals for you. You choose keys, store values, and use dictionary operations such as lookup, assignment, membership checks, and iteration.<\/p>\n<p>Dictionary keys must be hashable. Strings, numbers, booleans, and tuples of hashable values work well. Lists and dictionaries do not work as keys because they can change after insertion.<\/p>\n<p>Use dictionaries when data has natural labels: usernames, IDs, settings, counts, categories, cache keys, filenames, or any record that should be retrieved by a stable name.<\/p>\n<p>Modern Python dictionaries preserve insertion order as part of the language behavior. That means iteration follows the order keys were first added, but lookup is still based on keys rather than positions.<\/p>\n<p>Hash collisions are possible in theory, but Python dictionaries handle them internally. Application code should focus on choosing clear, stable keys and avoiding mutable key objects.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-hashmaps\/#Create_A_Hashmap_With_dict\" >Create A Hashmap With dict<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-hashmaps\/#Add_And_Update_Values\" >Add And Update Values<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-hashmaps\/#Check_For_Keys_Safely\" >Check For Keys Safely<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-hashmaps\/#Count_Items_With_A_Dictionary\" >Count Items With A Dictionary<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-hashmaps\/#Use_Tuples_As_Composite_Keys\" >Use Tuples As Composite Keys<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-hashmaps\/#Handle_Unhashable_Keys\" >Handle Unhashable Keys<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-hashmaps\/#How_A_Dictionary_Works_As_A_Hashmap\" >How A Dictionary Works As A Hashmap<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-hashmaps\/#Choose_Hashable_Keys\" >Choose Hashable Keys<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-hashmaps\/#Understand_Lookup_Collisions_And_Order\" >Understand Lookup, Collisions, And Order<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-hashmaps\/#Use_The_Right_Mapping_Operation\" >Use The Right Mapping Operation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/python-hashmaps\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/python-hashmaps\/#What_is_a_hashmap_in_Python\" >What is a hashmap in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/python-hashmaps\/#What_can_be_a_dictionary_key\" >What can be a dictionary key?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/python-hashmaps\/#Is_dictionary_lookup_O1_in_Python\" >Is dictionary lookup O(1) in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/python-hashmaps\/#How_does_Python_handle_hash_collisions\" >How does Python handle hash collisions?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Create_A_Hashmap_With_dict\"><\/span>Create A Hashmap With dict<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A dictionary literal uses braces with key-value pairs separated by colons.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">profile = {\n    \"name\": \"Ada\",\n    \"language\": \"Python\",\n    \"active\": True,\n}\n\nprint(profile[\"name\"])\nprint(profile[\"active\"])\n<\/code><\/pre>\n<\/div>\n<p>The keys are strings, and each key maps to one value. Looking up <code>profile[\"name\"]<\/code> returns the value stored for that key.<\/p>\n<p>Use clear key names that describe the data. A readable dictionary is often easier to maintain than several separate lists that must stay aligned by index.<\/p>\n<p>If each key describes one real-world entity, a dictionary can make later updates direct. Code can update one user&#8217;s status by key instead of searching through a list of records.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Add_And_Update_Values\"><\/span>Add And Update Values<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Assigning to a key adds a new pair when the key is missing and updates the pair when the key already exists.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">settings = {}\n\nsettings[\"theme\"] = \"light\"\nsettings[\"items_per_page\"] = 20\nsettings[\"theme\"] = \"dark\"\n\nprint(settings)\n<\/code><\/pre>\n<\/div>\n<p>The final dictionary keeps the latest value for <code>\"theme\"<\/code>. A dictionary cannot store two separate values for the same key at the same level.<\/p>\n<p>If a key should hold several values, store a list or set as the value. The key itself still remains unique.<\/p>\n<p>That distinction keeps the mapping simple. The dictionary maps one key to one object, and that object may itself contain several pieces of data.<\/p>\n<p><!-- Python Pool visual layout repair 2026-07-13 --><\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/hashmaps-dict-b195.png\" alt=\"Python Pool infographic showing keys, hash function, buckets, values, and lookup\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Python dictionaries map hashable keys to values through a hash-table design.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Check_For_Keys_Safely\"><\/span>Check For Keys Safely<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use <code>in<\/code> to test whether a key exists, or use <code>get()<\/code> when a fallback value is appropriate.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">scores = {\"Asha\": 91, \"Ben\": 84}\n\nprint(\"Asha\" in scores)\nprint(scores.get(\"Noah\", 0))\n\nif \"Ben\" in scores:\n    print(scores[\"Ben\"])\n<\/code><\/pre>\n<\/div>\n<p>This avoids a <code>KeyError<\/code> when a key is missing. Use direct indexing when a missing key means the program should fail loudly.<\/p>\n<p>Use <code>get()<\/code> for optional data, but do not hide missing required data with a default that looks real.<\/p>\n<p>For required configuration, direct indexing is often better because a missing key raises an immediate error. For optional settings, <code>get()<\/code> keeps the fallback near the lookup.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Count_Items_With_A_Dictionary\"><\/span>Count Items With A Dictionary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Dictionaries are useful for counting because each distinct item can become a key.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">words = [\"api\", \"python\", \"api\", \"data\", \"python\", \"api\"]\ncounts = {}\n\nfor word in words:\n    counts[word] = counts.get(word, 0) + 1\n\nprint(counts)\nprint(counts[\"api\"])\n<\/code><\/pre>\n<\/div>\n<p>The dictionary stores one counter per word. Each loop either starts a count at zero or increments the existing count.<\/p>\n<p>For production counting code, <code>collections.Counter<\/code> is often more compact, but the manual version shows the hashmap idea clearly.<\/p>\n<p>The same counting pattern works for grouped totals, status summaries, word frequencies, and simple analytics dashboards.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/hashmaps-lookup-b195.png\" alt=\"Python Pool infographic mapping a key through hashing, bucket selection, equality, and value\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Average constant-time lookup depends on good hashing and controlled collisions.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Use_Tuples_As_Composite_Keys\"><\/span>Use Tuples As Composite Keys<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A tuple can combine several pieces of data into one hashable key, as long as every item inside the tuple is hashable.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">inventory = {}\n\ninventory[(\"warehouse-a\", \"keyboard\")] = 12\ninventory[(\"warehouse-a\", \"mouse\")] = 30\ninventory[(\"warehouse-b\", \"keyboard\")] = 7\n\nprint(inventory[(\"warehouse-a\", \"keyboard\")])\nprint(sum(inventory.values()))\n<\/code><\/pre>\n<\/div>\n<p>This is useful when a lookup depends on more than one field. The tuple key keeps the lookup simple without nesting dictionaries.<\/p>\n<p>Use a small named tuple or dataclass when tuple positions become hard to remember. The key should stay readable.<\/p>\n<p>Composite keys are most helpful when the combination is stable and small. If the data grows into many fields, a nested dictionary or a proper record type may be easier to maintain.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Handle_Unhashable_Keys\"><\/span>Handle Unhashable Keys<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Lists are not valid dictionary keys because they are mutable. Convert list-like key data to a tuple when the contents should identify a value.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">lookup = {}\n\ntry:\n    lookup[[\"red\", \"blue\"]] = \"bad key\"\nexcept TypeError as error:\n    print(type(error).__name__)\n\nlookup[(\"red\", \"blue\")] = \"valid key\"\nprint(lookup[(\"red\", \"blue\")])\n<\/code><\/pre>\n<\/div>\n<p>The list key raises <code>TypeError<\/code>, while the tuple key works. That is because tuples are hashable when their contents are hashable.<\/p>\n<p>The practical rule is to use dictionaries when values should be found by stable keys. Keep keys hashable, use membership checks for optional data, and choose clear key names so the mapping explains itself.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/hashmaps-collision-b195.png\" alt=\"Python Pool infographic comparing hash collisions, probing, equality checks, and resolution\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Hash collisions are resolved with table rules and equality checks.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"How_A_Dictionary_Works_As_A_Hashmap\"><\/span>How A Dictionary Works As A Hashmap<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A hashmap uses a hash derived from a key to locate a storage position. Python&#8217;s dict hides that table and resize policy behind a small interface: assign a key, retrieve it, test membership, or remove it. The useful application-level decision is the key contract, not the table implementation.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">profile = {\n    \"name\": \"Ada\",\n    \"language\": \"Python\",\n}\n\nprofile[\"level\"] = \"advanced\"\nprint(profile[\"language\"])\nprint(\"name\" in profile)<\/code><\/pre>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Choose_Hashable_Keys\"><\/span>Choose Hashable Keys<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Dictionary keys must be hashable, which generally means their hash and equality behavior remain stable while they are stored. Strings, integers, booleans, and tuples containing hashable values are common. A list or dictionary is mutable and therefore cannot be used directly as a key.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">cache = {\n    (\"user\", 42): \"profile loaded\",\n    \"timeout\": 30,\n}\n\nprint(cache[(\"user\", 42)])\nprint(cache[\"timeout\"])<\/code><\/pre>\n<\/div>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/hashmaps-check-b195.png\" alt=\"Python Pool infographic testing hashability, mutation, resizing, ordering, and validation\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Check key hashability, mutation hazards, resizing, ordering guarantees, and workload assumptions.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Understand_Lookup_Collisions_And_Order\"><\/span>Understand Lookup, Collisions, And Order<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Dictionary lookup is average-case O(1), but that is a performance expectation rather than a guarantee for every adversarial workload. Different keys can hash to related positions; Python resolves collisions and resizes the table. Since modern Python preserves insertion order, iteration order is useful, but dictionary lookup is still key-based rather than index-based.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">counts = {\"python\": 2, \"data\": 1}\ncounts[\"python\"] += 1\ncounts[\"code\"] = 4\n\nfor key, value in counts.items():\n    print(key, value)<\/code><\/pre>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Use_The_Right_Mapping_Operation\"><\/span>Use The Right Mapping Operation<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use direct indexing when a missing key is a programming error, get() for optional values, setdefault() for one-time initialization, and collections.defaultdict or Counter when the data model is naturally a grouped collection or a frequency table. These choices make the missing-key policy visible.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from collections import Counter\n\nwords = [\"api\", \"python\", \"api\", \"python\"]\ncounts = Counter(words)\nprint(counts[\"api\"])\nprint(counts.most_common())<\/code><\/pre>\n<\/div>\n<p>Python&#8217;s official <a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#mapping-types-dict\">dict documentation<\/a> defines mapping operations, and the <a href=\"https:\/\/docs.python.org\/3\/glossary.html#term-hashable\">hashable glossary entry<\/a> explains which objects can be used as keys.<\/p>\n<p>For related mapping patterns, compare <a href=\"https:\/\/www.pythonpool.com\/python-dictionary-size\/\">dictionary size<\/a>, <a href=\"https:\/\/www.pythonpool.com\/python-key-value-pair\/\">key-value pairs<\/a>, and <a href=\"https:\/\/www.pythonpool.com\/python-ordereddict\/\">OrderedDict<\/a> when choosing a data structure.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions\"><\/span>Frequently Asked Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"What_is_a_hashmap_in_Python\"><\/span>What is a hashmap in Python?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>A Python dictionary is the standard hashmap-like mapping: it stores key-value pairs and uses a key to locate a value efficiently.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_can_be_a_dictionary_key\"><\/span>What can be a dictionary key?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>A key must be hashable and stable while it is in the dictionary. Strings, numbers, and tuples containing hashable values are common choices.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Is_dictionary_lookup_O1_in_Python\"><\/span>Is dictionary lookup O(1) in Python?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Dictionary lookup is average-case O(1), but the exact behavior depends on hashing, collisions, and the workload; it is not a universal worst-case guarantee.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_does_Python_handle_hash_collisions\"><\/span>How does Python handle hash collisions?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Python handles collisions internally. Application code should use well-behaved, immutable keys instead of trying to manage the table itself.<\/p>\n<p><script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"What is a hashmap in Python?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"A Python dictionary is the standard hashmap-like mapping: it stores key-value pairs and uses a key to locate a value efficiently.\"}},{\"@type\":\"Question\",\"name\":\"What can be a dictionary key?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"A key must be hashable and stable while it is in the dictionary. Strings, numbers, and tuples containing hashable values are common choices.\"}},{\"@type\":\"Question\",\"name\":\"Is dictionary lookup O(1) in Python?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Dictionary lookup is average-case O(1), but the exact behavior depends on hashing, collisions, and the workload; it is not a universal worst-case guarantee.\"}},{\"@type\":\"Question\",\"name\":\"How does Python handle hash collisions?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Python handles collisions internally. Application code should use well-behaved, immutable keys instead of trying to manage the table itself.\"}}]}<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understand Python hashmaps through dictionaries, hashing, key-value storage, lookup behavior, updates, and collision-aware design.<\/p>\n","protected":false},"author":3,"featured_media":34218,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[1128,1145,1133,1141,1137,1124,1125,1127,1132,1120,1139,1126,1138,1144,1136,1121,1131,1130,1135,1122,1134,1123,1129,1140,1143],"class_list":["post-3329","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-create-hashmap-in-python","tag-create-hashmap-python","tag-custom-sorting-dictionary-python-pass-hashmap","tag-difference-between-dictionary-and-hashmap-python","tag-hashmap-equivalent-in-python","tag-hashmap-implementation-python","tag-hashmap-in-python","tag-hashmap-in-python-geeksforgeeks","tag-hashmap-put-in-python","tag-hashmap-python","tag-hashmap-python-implementation","tag-hashmap-vs-dictionary-python","tag-hashmap-with-multiple-keys-python","tag-implement-a-hashmap-in-python","tag-implement-hashmap-in-python","tag-python-dictionary-vs-hashmap","tag-python-hashmap","tag-python-hashmap-contains","tag-python-hashmap-example","tag-python-hashmap-implementation","tag-python-hashmap-keys-to-array","tag-python-hashmap-list","tag-python-hashmap-remove","tag-python-hashmap-vs-dictionary","tag-sort-hashmap-by-value-python","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v28.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Hashmaps: Dictionaries, Hashing, and O(1) Lookup<\/title>\n<meta name=\"description\" content=\"Understand Python hashmaps through dictionaries, hashing, key-value storage, lookup behavior, updates, and collision-aware design.\" \/>\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.pythonpool.com\/python-hashmaps\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Hashmaps: Dictionaries, Hashing, and O(1) Lookup\" \/>\n<meta property=\"og:description\" content=\"Understand Python hashmaps through dictionaries, hashing, key-value storage, lookup behavior, updates, and collision-aware design.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-hashmaps\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-23T11:36:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-07-13T06:56:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-hashmaps-2.png\" \/>\n<meta name=\"author\" content=\"Ashwini Mandani\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Python Pool\" \/>\n<meta name=\"twitter:description\" content=\"Practical Python tutorials, error fixes, code examples, and project guides.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-hashmaps-2.png\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ashwini Mandani\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-hashmaps\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-hashmaps\\\/\"},\"author\":{\"name\":\"Ashwini Mandani\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/8a75fb48eb1f3dc952df65a8c28ac056\"},\"headline\":\"Python Hashmaps: Dictionaries, Hashing, and O(1) Lookup\",\"datePublished\":\"2020-06-23T11:36:55+00:00\",\"dateModified\":\"2026-07-13T06:56:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-hashmaps\\\/\"},\"wordCount\":1186,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-hashmaps\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-hashmaps-dictionaries-guide-pythonpool.png\",\"keywords\":[\"create hashmap in python\",\"create hashmap python\",\"custom sorting dictionary python pass hashmap\",\"difference between dictionary and hashmap python\",\"hashmap equivalent in python\",\"hashmap implementation python\",\"hashmap in python\",\"hashmap in python geeksforgeeks\",\"hashmap put in python\",\"hashmap python\",\"hashmap python implementation\",\"hashmap vs dictionary python\",\"hashmap with multiple keys python\",\"implement a hashmap in python\",\"implement hashmap in python\",\"python dictionary vs hashmap\",\"python hashmap\",\"python hashmap contains\",\"python hashmap example\",\"python hashmap implementation\",\"python hashmap keys to array\",\"python hashmap list\",\"python hashmap remove\",\"python hashmap vs dictionary\",\"sort hashmap by value python\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/python-hashmaps\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-hashmaps\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/python-hashmaps\\\/\",\"name\":\"Python Hashmaps: Dictionaries, Hashing, and O(1) Lookup\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-hashmaps\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-hashmaps\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-hashmaps-dictionaries-guide-pythonpool.png\",\"datePublished\":\"2020-06-23T11:36:55+00:00\",\"dateModified\":\"2026-07-13T06:56:56+00:00\",\"description\":\"Understand Python hashmaps through dictionaries, hashing, key-value storage, lookup behavior, updates, and collision-aware design.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-hashmaps\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/python-hashmaps\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-hashmaps\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-hashmaps-dictionaries-guide-pythonpool.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-hashmaps-dictionaries-guide-pythonpool.png\",\"width\":1350,\"height\":650,\"caption\":\"Python hashmaps with dictionaries guide\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-hashmaps\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pythonpool.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Hashmaps: Dictionaries, Hashing, and O(1) Lookup\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"name\":\"Python Pool\",\"description\":\"Practical Python tutorials, error fixes, code examples, and project guides.\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pythonpool.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/x.com\\\/pythonpool\",\"https:\\\/\\\/www.youtube.com\\\/c\\\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/8a75fb48eb1f3dc952df65a8c28ac056\",\"name\":\"Ashwini Mandani\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"caption\":\"Ashwini Mandani\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Hashmaps: Dictionaries, Hashing, and O(1) Lookup","description":"Understand Python hashmaps through dictionaries, hashing, key-value storage, lookup behavior, updates, and collision-aware design.","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.pythonpool.com\/python-hashmaps\/","og_locale":"en_US","og_type":"article","og_title":"Python Hashmaps: Dictionaries, Hashing, and O(1) Lookup","og_description":"Understand Python hashmaps through dictionaries, hashing, key-value storage, lookup behavior, updates, and collision-aware design.","og_url":"https:\/\/www.pythonpool.com\/python-hashmaps\/","og_site_name":"Python Pool","article_published_time":"2020-06-23T11:36:55+00:00","article_modified_time":"2026-07-13T06:56:56+00:00","og_image":[{"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-hashmaps-2.png","type":"","width":"","height":""}],"author":"Ashwini Mandani","twitter_card":"summary_large_image","twitter_title":"Python Pool","twitter_description":"Practical Python tutorials, error fixes, code examples, and project guides.","twitter_image":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-hashmaps-2.png","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Ashwini Mandani","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-hashmaps\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-hashmaps\/"},"author":{"name":"Ashwini Mandani","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056"},"headline":"Python Hashmaps: Dictionaries, Hashing, and O(1) Lookup","datePublished":"2020-06-23T11:36:55+00:00","dateModified":"2026-07-13T06:56:56+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-hashmaps\/"},"wordCount":1186,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-hashmaps\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-hashmaps-dictionaries-guide-pythonpool.png","keywords":["create hashmap in python","create hashmap python","custom sorting dictionary python pass hashmap","difference between dictionary and hashmap python","hashmap equivalent in python","hashmap implementation python","hashmap in python","hashmap in python geeksforgeeks","hashmap put in python","hashmap python","hashmap python implementation","hashmap vs dictionary python","hashmap with multiple keys python","implement a hashmap in python","implement hashmap in python","python dictionary vs hashmap","python hashmap","python hashmap contains","python hashmap example","python hashmap implementation","python hashmap keys to array","python hashmap list","python hashmap remove","python hashmap vs dictionary","sort hashmap by value python"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-hashmaps\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/python-hashmaps\/","url":"https:\/\/www.pythonpool.com\/python-hashmaps\/","name":"Python Hashmaps: Dictionaries, Hashing, and O(1) Lookup","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-hashmaps\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-hashmaps\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-hashmaps-dictionaries-guide-pythonpool.png","datePublished":"2020-06-23T11:36:55+00:00","dateModified":"2026-07-13T06:56:56+00:00","description":"Understand Python hashmaps through dictionaries, hashing, key-value storage, lookup behavior, updates, and collision-aware design.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-hashmaps\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-hashmaps\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-hashmaps\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-hashmaps-dictionaries-guide-pythonpool.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-hashmaps-dictionaries-guide-pythonpool.png","width":1350,"height":650,"caption":"Python hashmaps with dictionaries guide"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-hashmaps\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Python Hashmaps: Dictionaries, Hashing, and O(1) Lookup"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Practical Python tutorials, error fixes, code examples, and project guides.","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056","name":"Ashwini Mandani","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","caption":"Ashwini Mandani"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=3329"}],"version-history":[{"count":16,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3329\/revisions"}],"predecessor-version":[{"id":41370,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3329\/revisions\/41370"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/34218"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=3329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=3329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=3329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}