{"id":34225,"date":"2026-10-07T17:18:59","date_gmt":"2026-10-07T17:18:59","guid":{"rendered":"https:\/\/www.askpython.com\/?p=34225"},"modified":"2026-10-07T17:18:59","modified_gmt":"2026-10-07T17:18:59","slug":"initialize-python-dictionary","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/dictionary\/initialize-python-dictionary","title":{"rendered":"Initialize Dictionary in Python"},"content":{"rendered":"<!-- article-type: tutorial -->\n<!-- receipt-backed-first-person -->\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<p class=\"wp-block-paragraph\">You have values to store under keys, and you&#8217;re choosing how to create the dictionary that will hold them. A Python dictionary stores values under keys, and you can create one by writing pairs or pairing separate lists of keys and values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;ll show you how to initialize a dictionary from known pairs or separate key and value lists, then how to give each key its own list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">TL;DR: Initialize a Python Dictionary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use a dictionary literal for known pairs, dict() for pair data, and a comprehension when each key needs a newly computed value.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n\n<li>Start an empty dictionary with empty braces.<\/li>\n\n\n<li>Use zip() when separate iterables contain matching keys and values.<\/li>\n\n\n<li>Use dict.fromkeys() for a shared immutable default, not for separate lists.<\/li>\n\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nsettings = {&quot;theme&quot;: &quot;dark&quot;, &quot;retries&quot;: 3}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">What Is a Python Dictionary?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A Python dictionary, or dict, holds key-value pairs and is mutable. You can add an entry or replace its value after creation, even if you began with an empty dictionary.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A key must be unique and hashable, which lets Python find its entry consistently. A repeated key updates its value.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Part<\/th><th>Role<\/th><\/tr><\/thead><tbody><tr><td>Key<\/td><td>Names or identifies an entry<\/td><\/tr><tr><td>Value<\/td><td>Stores the data associated with that key<\/td><\/tr><tr><td>Pair<\/td><td>Connects one key to one value<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">For example, a settings dictionary can map the key theme to the value dark. A value may itself be a list or another dictionary, so choose its type to match how the program will use it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#dict\" target=\"_blank\" rel=\"noopener\">Python&#8217;s dict documentation<\/a> guarantees insertion order from version 3.7 onward. I checked that version history. Updating a value does not move its key.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use len() to count entries. Items inside a list value do not count as separate dictionary entries.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Initialize a Dictionary in Python Step by Step<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Choose the initializer from the shape of the data you already have. The examples below run as Python files and show the resulting dictionaries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Use a literal or dict() for known pairs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Write pairs directly inside braces when the keys and values are already known. Each key is followed by a colon and its value. So the dict() constructor is useful when the pairs arrive as keyword arguments or as an iterable of two-item pairs.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nempty = {}\nsettings = {&quot;theme&quot;: &quot;dark&quot;, &quot;retries&quot;: 3}\nfrom_keywords = dict(name=&quot;Ada&quot;, language=&quot;Python&quot;)\nfrom_pairs = dict(&#x5B;(&quot;mode&quot;, &quot;dark&quot;), (&quot;retries&quot;, 3)])\n\nprint(&quot;empty:&quot;, empty)\nprint(&quot;literal:&quot;, settings)\nprint(&quot;keyword arguments:&quot;, from_keywords)\nprint(&quot;pair iterable:&quot;, from_pairs)\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nempty: {}\nliteral: {&#039;theme&#039;: &#039;dark&#039;, &#039;retries&#039;: 3}\nkeyword arguments: {&#039;name&#039;: &#039;Ada&#039;, &#039;language&#039;: &#039;Python&#039;}\npair iterable: {&#039;mode&#039;: &#039;dark&#039;, &#039;retries&#039;: 3}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">I ran python3 initialize_known_dict.py and got four dictionaries, including an empty one. Keyword arguments require valid identifier names, so use pair data when a key contains a space or is not a string. Both empty braces and dict() create an empty dictionary.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/initialize-known-dict.png\" alt=\"Terminal output for an empty dictionary, a literal, and the dict constructor\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Pair parallel iterables with zip()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When one iterable contains keys and another contains their values in the same order, zip() pairs items by position. So pass those pairs to dict().<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nkeys = &#x5B;&quot;name&quot;, &quot;role&quot;]\nvalues = &#x5B;&quot;Ada&quot;, &quot;engineer&quot;]\nprofile = dict(zip(keys, values))\nprices = {item: price for item, price in &#x5B;(&quot;tea&quot;, 4), (&quot;coffee&quot;, 5)]}\n\nprint(&quot;from zip():&quot;, profile)\nprint(&quot;from comprehension:&quot;, prices)\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfrom zip(): {&#039;name&#039;: &#039;Ada&#039;, &#039;role&#039;: &#039;engineer&#039;}\nfrom comprehension: {&#039;tea&#039;: 4, &#039;coffee&#039;: 5}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">I ran python3 initialize_from_iterables.py. The output pairs each name with its value, while the comprehension creates a price for each item.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/initialize-from-iterables.png\" alt=\"Terminal output for dictionaries built with zip and a comprehension\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Create separate mutable defaults with a comprehension<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use dict.fromkeys() when every key should refer to the same immutable default, such as a status string. In practice, a dictionary comprehension gives each key a fresh list.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nkeys = &#x5B;&quot;north&quot;, &quot;south&quot;]\nshared_labels = dict.fromkeys(keys, &quot;pending&quot;)\nshared_lists = dict.fromkeys(keys, &#x5B;])\nshared_lists&#x5B;&quot;north&quot;].append(&quot;item&quot;)\nindependent_lists = {key: &#x5B;] for key in keys}\nindependent_lists&#x5B;&quot;north&quot;].append(&quot;item&quot;)\n\nprint(&quot;fromkeys with a string:&quot;, shared_labels)\nprint(&quot;shared mutable list:&quot;, shared_lists)\nprint(&quot;separate lists:&quot;, independent_lists)\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfromkeys with a string: {&#039;north&#039;: &#039;pending&#039;, &#039;south&#039;: &#039;pending&#039;}\nshared mutable list: {&#039;north&#039;: &#x5B;&#039;item&#039;], &#039;south&#039;: &#x5B;&#039;item&#039;]}\nseparate lists: {&#039;north&#039;: &#x5B;&#039;item&#039;], &#039;south&#039;: &#x5B;]}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">I ran python3 initialize_defaults.py to compare both cases. Adding one item through north changes south too when fromkeys() receives one list, because both keys refer to that same list. Use the comprehension when each key needs independent contents.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/initialize-defaults.png\" alt=\"Terminal output comparing shared and separate mutable dictionary values\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Dictionary Initialization Edge Cases<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Check what happens when input keys or values repeat, or when a default can be changed. These details affect the dictionary you end up with.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Case<\/th><th>Behavior<\/th><th>Choice<\/th><\/tr><\/thead><tbody><tr><td>Unequal inputs to zip()<\/td><td>Pairing stops when the shorter iterable ends<\/td><td>Check lengths first if every item needs a partner<\/td><\/tr><tr><td>Repeated key<\/td><td>The later value replaces the earlier one<\/td><td>Check for duplicate keys when input order matters<\/td><\/tr><tr><td>List used as a key<\/td><td>Raises TypeError because lists are unhashable<\/td><td>Use a string, number, or suitable tuple<\/td><\/tr><tr><td>Mutable value in fromkeys()<\/td><td>Every key refers to the same object<\/td><td>Use a comprehension for separate lists<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">With zip(), unmatched input items disappear. Check both iterable lengths whenever every item needs a partner. Pairing three keys with two values produces only two pairs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion: Choose the Input Shape<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">So choose braces for known pairs, dict() for pair data, zip() for parallel inputs, or a comprehension for calculated or independent values. Each example uses built-in types and needs no third-party package.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For dictionary lookups and updates, read the <a href=\"https:\/\/www.askpython.com\/python\/dictionary\/python-dictionary-dict-tutorial\">Python dictionary tutorial<\/a>. If your source is a pair of lists, see <a href=\"https:\/\/www.askpython.com\/python\/list\/convert-list-to-a-dictionary\">how to convert a list to a dictionary<\/a>. The <a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#dict\" target=\"_blank\" rel=\"noopener\">Python dict documentation<\/a> describes the built-in methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQ<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These answers cover two common follow-up questions about starting with keys or creating state for an object.<\/p>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1783199152863\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Can I initialize a dictionary inside __init__?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes. Assign a new dictionary to self inside __init__ when each instance needs its own state. This keeps one instance&#8217;s changes separate from another&#8217;s.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1783199152864\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What value does dict.fromkeys() use by default?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>It uses None when you omit the value argument. Each key then maps to None. If you pass a mutable object such as a list, all keys refer to that same object.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>You have values to store under keys, and you&#8217;re choosing how to create the dictionary that will hold them. A Python dictionary stores values under keys, and you can create one by writing pairs or pairing separate lists of keys and values. I&#8217;ll show you how to initialize a dictionary from known pairs or separate [&hellip;]<\/p>\n","protected":false},"author":47,"featured_media":71955,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-34225","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dictionary"],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/34225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=34225"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/34225\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/71955"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=34225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=34225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=34225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}