{"id":4988,"date":"2017-12-15T22:26:32","date_gmt":"2017-12-15T16:56:32","guid":{"rendered":"https:\/\/java2blog.com\/?p=4988"},"modified":"2023-11-25T17:08:14","modified_gmt":"2023-11-25T11:38:14","slug":"python-dict-setdefault","status":"publish","type":"post","link":"https:\/\/java2blog.com\/python-dict-setdefault\/","title":{"rendered":"Python dict setDefault"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Syntax\">Syntax<\/a><\/li><li><a href=\"#Return\">Return<\/a><\/li><li><a href=\"#Python_dictionary_setDefault_example\">Python dictionary setDefault example<\/a><ul><li><a href=\"#Example_1_If_key_is_present_in_dictionary\">Example 1: If key is present in dictionary<\/a><\/li><li><a href=\"#Example_2_If_key_is_present_in_dictionary_but_default_value_is_not_provided\">Example 2: If key is present in dictionary but default value is not provided<\/a><\/li><li><a href=\"#Example_2_If_key_is_present_in_dictionary_and_default_value_is_provided\">Example 2: If key is present in dictionary and default value is provided<\/a><\/li><\/ul><\/li><\/ul><\/div>\n<p>In this tutorial, we will see about Python dict setDefault method.<br \/>\nPython dictionary setDefault method is used to return the value if present in dictionary else inserts the key with value in dictionary if default value is provided.<\/p>\n<h2><span id=\"Syntax\"><span style=\"color: #f89820;\">Syntax<\/span><\/span><\/h2>\n<pre class=\"python\" name=\"code\">\ndict.setdefault(key[, default_value])\n<\/pre>\n<h2><span id=\"Return\"><span style=\"color: #f89820;\">Return<\/span><\/span><\/h2>\n<p>It returns the value if key exists in dictionary else it inserts key with value.<\/p>\n<h2><span id=\"Python_dictionary_setDefault_example\"><span style=\"color: #f89820;\">Python dictionary setDefault example<\/span><\/span><\/h2>\n<h3><span id=\"Example_1_If_key_is_present_in_dictionary\">Example 1: If key is present in dictionary<\/span><\/h3>\n<pre code=\"python\">\nlistOfCountries={\"India\":\"Delhi\",\"China\":\"Beijing\",\"Australia\":\"Canberra\",\"UK\":\"London\"}\n\n#If key is present in dictionary\ncapital=listOfCountries.setdefault(\"China\")\n\nprint(\"Capital of china:\",capital)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nCapital of china: Beijing\n<\/div>\n<h3><span id=\"Example_2_If_key_is_present_in_dictionary_but_default_value_is_not_provided\">Example 2: If key is present in dictionary but default value is not provided<\/span><\/h3>\n<pre code=\"python\">\nlistOfCountries={\"India\":\"Delhi\",\"China\":\"Beijing\",\"Australia\":\"Canberra\",\"UK\":\"London\"}\n\n#If key is not present in dictionary and default is not provided\ncapital=listOfCountries.setdefault(\"USA\")\n\nprint(\"Capital of USA:\",capital)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nCapital of USA: None\n<\/div>\n<h3><span id=\"Example_2_If_key_is_present_in_dictionary_and_default_value_is_provided\">Example 2: If key is present in dictionary and default value is provided<\/span><\/h3>\n<pre code=\"python\">\nlistOfCountries={\"India\":\"Delhi\",\"China\":\"Beijing\",\"Australia\":\"Canberra\",\"UK\":\"London\"}\n\n#If key is not present in dictionary and default is provided\ncapital=listOfCountries.setdefault(\"USA\",\"washigton\")\n\nprint(\"Capital of USA:\",capital)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nCapital of USA: washigton\n<\/div>\n<p>That&#8217;s all about Python dict setDefault method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsSyntaxReturnPython dictionary setDefault exampleExample 1: If key is present in dictionaryExample 2: If key is present in dictionary but default value is not providedExample 2: If key is present in dictionary and default value is provided In this tutorial, we will see about Python dict setDefault method. Python dictionary setDefault method is used [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[145],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/4988"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=4988"}],"version-history":[{"count":1,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/4988\/revisions"}],"predecessor-version":[{"id":25893,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/4988\/revisions\/25893"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=4988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=4988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=4988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}