{"id":15904,"date":"2020-05-12T23:47:35","date_gmt":"2020-05-13T06:47:35","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=15904"},"modified":"2023-12-01T02:45:20","modified_gmt":"2023-12-01T10:45:20","slug":"python-dictionary-update","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/","title":{"rendered":"Python Dictionary Update"},"content":{"rendered":"\n<p>Updating the contents of a dictionary is a common operation when working with dictionaries.<br><\/p>\n\n\n\n<p>For instance, suppose you have a dictionary that stores a list of car prices. If you want to apply a discount to one of the cars in the dictionary, you would want to update an item in your dictionary.<br><\/p>\n\n\n\n<p>That\u2019s where the Python dictionary <code>update()<\/code> method comes in. This tutorial will discuss, with reference to an example, how to use the dictionary <code>update()<\/code> method to update an item in a Python dictionary.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Dictionaries: A Refresher<\/h2>\n\n\n\n<p>In Python, dictionaries are referred to as the mapping data type.<br><\/p>\n\n\n\n<p>This is because dictionaries add keys to values and create a key-value pair, which is used to store related data. Here is an example of a dictionary in Python:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>book = {\n\t'title': 'The Great Gatsby',\n\t'published': '1925',\n\t'author': 'F. Scott Fitzgerald',\n\t'in_stock': False\n}<\/pre><\/div>\n\n\n\n<p>This dictionary contains four keys (which appear to the left of the colons) and values (which appear to the right of the colons).<br><\/p>\n\n\n\n<p>Now, suppose we want to change the value of the <code>in_stock<\/code> item in our dictionary to True. We could do so using the dictionary <code>update()<\/code> method.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Dictionary update()<\/h2>\n\n\n\n<p>The dictionary <code>update()<\/code> method allows you to update the contents of a dictionary.<br><\/p>\n\n\n\n<p>Here is the syntax for the update method:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>dictionary_name.update(new_item)<\/pre><\/div>\n\n\n\n<p>The value <code>new_item<\/code> should be a dictionary with key-value pairs that replaces an existing item in your dictionary.&nbsp;<br><\/p>\n\n\n\n<p>If the new item you specify does not already exist in the dictionary, it will be added to the dictionary.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Update an Item in a Dictionary<\/h3>\n\n\n\n<p>Suppose we are the owner of a bookstore and we have a dictionary that stores information on the book <code>The Great Gatsby<\/code>. We want to update the <code>in_stock<\/code> item in this dictionary because we have just received a new delivery of this book.<br><\/p>\n\n\n\n<p>We could update our dictionary using the following program:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>book = {\n\t'title': 'The Great Gatsby',\n\t'published': '1925',\n\t'author': 'F. Scott Fitzgerald',\n\t'in_stock': False\n}\nnew_stock = { 'in_stock': True }\nbook.update(new_stock)\nprint(book)<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>{'title': 'The Great Gatsby', 'published': '1925', 'author': 'F. Scott Fitzgerald', 'in_stock': True}<\/pre><\/div>\n\n\n\n<p>Let\u2019s break down our code. First, we have declared a dictionary called <code>book<\/code> which stores information about the book <code>The Great Gatsby<\/code>.<br><\/p>\n\n\n\n<p>Then, we have declared a variable with the revision we want to make to our dictionary. In this case, we have created a dictionary that sets the value of <code>in_stock<\/code> to True.<br><\/p>\n\n\n\n<p>Next, we use the <code>update()<\/code> method to push the contents of <code>new_stock<\/code> to our <code>book<\/code> list. This allows us to update <code>new_stock<\/code> to be equal to True in our <code>book<\/code> dictionary. Finally, we print out our revised dictionary using print(book).<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The Python dictionary <code>update()<\/code> method allows you to update the contents of a dictionary.<br><\/p>\n\n\n\n<p>This tutorial discussed, with reference to an example, how to use the dictionary <code>update()<\/code> method to update an item in a dictionary. Now you\u2019re equipped with the knowledge you need to update the contents of a Python dictionary like a professional developer!<\/p>\n","protected":false},"excerpt":{"rendered":"Updating the contents of a dictionary is a common operation when working with dictionaries. For instance, suppose you have a dictionary that stores a list of car prices. If you want to apply a discount to one of the cars in the dictionary, you would want to update an item in your dictionary. That\u2019s where&hellip;","protected":false},"author":240,"featured_media":15905,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-15904","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 Dictionary Update: A Complete Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"The dictionary update() method allows you to update the contents of a Python dictionary. On Career Karma, learn how to use the dictionary update() method.\" \/>\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-dictionary-update\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Dictionary Update\" \/>\n<meta property=\"og:description\" content=\"The dictionary update() method allows you to update the contents of a Python dictionary. On Career Karma, learn how to use the dictionary update() method.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/\" \/>\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-13T06:47:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T10:45:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-close-up-code-computer-546819-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"677\" \/>\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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Python Dictionary Update\",\"datePublished\":\"2020-05-13T06:47:35+00:00\",\"dateModified\":\"2023-12-01T10:45:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/\"},\"wordCount\":439,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-close-up-code-computer-546819-1.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/\",\"name\":\"Python Dictionary Update: A Complete Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-close-up-code-computer-546819-1.jpg\",\"datePublished\":\"2020-05-13T06:47:35+00:00\",\"dateModified\":\"2023-12-01T10:45:20+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"The dictionary update() method allows you to update the contents of a Python dictionary. On Career Karma, learn how to use the dictionary update() method.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-close-up-code-computer-546819-1.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-close-up-code-computer-546819-1.jpg\",\"width\":1020,\"height\":677},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/#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 Dictionary Update\"}]},{\"@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 Dictionary Update: A Complete Guide | Career Karma","description":"The dictionary update() method allows you to update the contents of a Python dictionary. On Career Karma, learn how to use the dictionary update() method.","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-dictionary-update\/","og_locale":"en_US","og_type":"article","og_title":"Python Dictionary Update","og_description":"The dictionary update() method allows you to update the contents of a Python dictionary. On Career Karma, learn how to use the dictionary update() method.","og_url":"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-05-13T06:47:35+00:00","article_modified_time":"2023-12-01T10:45:20+00:00","og_image":[{"width":1020,"height":677,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-close-up-code-computer-546819-1.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":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Python Dictionary Update","datePublished":"2020-05-13T06:47:35+00:00","dateModified":"2023-12-01T10:45:20+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/"},"wordCount":439,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-close-up-code-computer-546819-1.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-dictionary-update\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/","url":"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/","name":"Python Dictionary Update: A Complete Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-close-up-code-computer-546819-1.jpg","datePublished":"2020-05-13T06:47:35+00:00","dateModified":"2023-12-01T10:45:20+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"The dictionary update() method allows you to update the contents of a Python dictionary. On Career Karma, learn how to use the dictionary update() method.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-dictionary-update\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-close-up-code-computer-546819-1.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/blur-close-up-code-computer-546819-1.jpg","width":1020,"height":677},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-dictionary-update\/#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 Dictionary Update"}]},{"@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\/15904","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=15904"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/15904\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/15905"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=15904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=15904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=15904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}