{"id":17367,"date":"2020-05-24T23:23:42","date_gmt":"2020-05-25T06:23:42","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=17367"},"modified":"2023-12-01T02:48:13","modified_gmt":"2023-12-01T10:48:13","slug":"python-add-to-dictionary","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/","title":{"rendered":"Python Add to Dictionary: A Guide"},"content":{"rendered":"\n<p>Dictionaries are used to store data using the key-value structure in Python.<br><\/p>\n\n\n\n<p>When you\u2019re working with dictionaries, you may ask yourself, how can I add an item to a dictionary? This article will answer that question.<br><\/p>\n\n\n\n<p>We\u2019ll break down the basics of dictionaries, how they work, and how you can add an item to a Python dictionary. By the end of this tutorial, you\u2019ll be an expert at adding items to Python dictionaries.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Dictionary: A Refresher<\/h2>\n\n\n\n<p>The dictionary data structure allows you to map keys to values. This is useful because keys act as a label for a value, which means that when you want to access a particular value, all you have to do is reference the key name for the value you want to retrieve.<br><\/p>\n\n\n\n<p>Dictionaries generally store information that is related in some way, such as a record for a student at school, or a list of colors in which you can purchase a rug.<br><\/p>\n\n\n\n<p>Consider the following dictionary:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>scones = {\n\t\"Fruit\": 22,\n\t\"Plain\": 14,\n\t\"Cinnamon\": 4,\n\t\"Cheese\": 21\n}<\/pre><\/div>\n\n\n\n<p>This dictionary contains four keys and four values. The keys are stored as a string, and appear to the left of the colon; the values are stored to the right of the colons; bound to a particular key in the dictionary.<br><\/p>\n\n\n\n<p>Now, how do we add an item to this dictionary? Let\u2019s break it down.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Add to Dictionary<\/h2>\n\n\n\n<p>To add an item to a Python dictionary, you should assign a value to a new index key in your dictionary.<br><\/p>\n\n\n\n<p>Unlike lists and tuples, there is no <code>add()<\/code>, <code>insert()<\/code>, or <code>append()<\/code> method that you can use to add items to your data structure. Instead, you have to create a new index key, which will then be used to store the value you want to store in your dictionary.<br><\/p>\n\n\n\n<p>Here is the syntax for adding a new value to a dictionary:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>dictionary_name[key] = value<\/pre><\/div>\n\n\n\n<p>Let\u2019s use an example to illustrate how to add an item to a dictionary. In our dictionary from earlier, we stored four keys and values. This dictionary reflected the stock levels of different flavors of scones sold at a cafe.<br><\/p>\n\n\n\n<p>Suppose we have just baked a batch of 10 new cherry scones. Now, we want to add these scones to our dictionary of scone types. To do so, we can use this code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>scones = {\n\t\"Fruit\": 22,\n\t\"Plain\": 14,\n\t\"Cinnamon\": 4,\n\t\"Cheese\": 21\n}\nscones[\"Cherry\"] = 10\nprint(scones)<\/pre><\/div>\n\n\n\n<p>Our code returns:<br><\/p>\n\n\n\n<p><code>{'Fruit': 22, 'Plain': 14, 'Cinnamon': 4, 'Cheese': 21, 'Cherry': 10}<\/code><\/p>\n\n\n\n<p>As you can see, our code has added the \u201cCherry\u201d key to our dictionary, which has been assigned the value 10.<br><\/p>\n\n\n\n<p>In our code, we first declared a dictionary called \u201cscones\u201d which stores information on how many scones are available at a cafe to order. Next, we added the key \u201cCherry\u201d to our dictionary and assigned it the value 10 by using the following code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>scones[\"Cherry\"] = 10<\/pre><\/div>\n\n\n\n<p>Finally, we printed out the updated version of our dictionary.<br><\/p>\n\n\n\n<p>Notice that, in the output of our code, our dictionary is not ordered. This is because data stored in a dictionary, unlike data stored in a list, does not have any particular order.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Tip: Adding and Updating Use the Same Notation<\/h2>\n\n\n\n<p>The same notation can be used to update a value in a dictionary. Suppose we have just baked a new batch of 10 cinnamon scones. We could update our dictionary to reflect this using the following line of code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>scones = {\n\t\"Fruit\": 22,\n\t\"Plain\": 14,\n\t\"Cinnamon\": 4,\n\t\"Cheese\": 21\n}\nscones[\"Cinnamon\"] = 14\nprint(scones)<\/pre><\/div>\n\n\n\n<p>Our code returns:<br><\/p>\n\n\n\n<p><code>{'Fruit': 22, 'Plain': 14, 'Cinnamon': 14, 'Cheese': 21}<\/code><br><\/p>\n\n\n\n<p>This line of code sets the value associated with the \u201cCinnamon\u201d key in our dictionary to 14.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>There is no <code>add()<\/code>, <code>append()<\/code>, or <code>insert()<\/code> method you can use to add an item to a dictionary in Python. Instead, you add an item to a dictionary by inserting a new index key into the dictionary, then assigning it a particular value.<br><\/p>\n\n\n\n<p>This tutorial discussed, with an example, how to add an item to a Python dictionary. Now you\u2019re ready to start adding items to dictionaries like a Python expert!<\/p>\n","protected":false},"excerpt":{"rendered":"Dictionaries are used to store data using the key-value structure in Python. When you\u2019re working with dictionaries, you may ask yourself, how can I add an item to a dictionary? This article will answer that question. We\u2019ll break down the basics of dictionaries, how they work, and how you can add an item to a&hellip;","protected":false},"author":240,"featured_media":17368,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-17367","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":"","is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Add to Dictionary: A Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"To add an item to a dictionary in Python, you need to assign a value to a new key. On Career Karma, learn how to add an item to a Python dictionary.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Add to Dictionary: A Guide\" \/>\n<meta property=\"og:description\" content=\"To add an item to a dictionary in Python, you need to assign a value to a new key. On Career Karma, learn how to add an item to a Python dictionary.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-25T06:23:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T10:48:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/person-using-silver-macbook-pro-1181467.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"681\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Python Add to Dictionary: A Guide\",\"datePublished\":\"2020-05-25T06:23:42+00:00\",\"dateModified\":\"2023-12-01T10:48:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/\"},\"wordCount\":627,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/person-using-silver-macbook-pro-1181467.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/\",\"name\":\"Python Add to Dictionary: A Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/person-using-silver-macbook-pro-1181467.jpg\",\"datePublished\":\"2020-05-25T06:23:42+00:00\",\"dateModified\":\"2023-12-01T10:48:13+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"To add an item to a dictionary in Python, you need to assign a value to a new key. On Career Karma, learn how to add an item to a Python dictionary.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/person-using-silver-macbook-pro-1181467.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/person-using-silver-macbook-pro-1181467.jpg\",\"width\":1020,\"height\":681},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/careerkarma.com\/blog\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Add to Dictionary: A Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Add to Dictionary: A Guide | Career Karma","description":"To add an item to a dictionary in Python, you need to assign a value to a new key. On Career Karma, learn how to add an item to a Python dictionary.","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:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/","og_locale":"en_US","og_type":"article","og_title":"Python Add to Dictionary: A Guide","og_description":"To add an item to a dictionary in Python, you need to assign a value to a new key. On Career Karma, learn how to add an item to a Python dictionary.","og_url":"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-05-25T06:23:42+00:00","article_modified_time":"2023-12-01T10:48:13+00:00","og_image":[{"width":1020,"height":681,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/person-using-silver-macbook-pro-1181467.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Python Add to Dictionary: A Guide","datePublished":"2020-05-25T06:23:42+00:00","dateModified":"2023-12-01T10:48:13+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/"},"wordCount":627,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/person-using-silver-macbook-pro-1181467.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/","url":"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/","name":"Python Add to Dictionary: A Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/person-using-silver-macbook-pro-1181467.jpg","datePublished":"2020-05-25T06:23:42+00:00","dateModified":"2023-12-01T10:48:13+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"To add an item to a dictionary in Python, you need to assign a value to a new key. On Career Karma, learn how to add an item to a Python dictionary.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/person-using-silver-macbook-pro-1181467.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/person-using-silver-macbook-pro-1181467.jpg","width":1020,"height":681},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-add-to-dictionary\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"Python Add to Dictionary: A Guide"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/17367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=17367"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/17367\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/17368"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=17367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=17367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=17367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}