{"componentChunkName":"component---node-modules-smooth-doc-src-templates-doc-js","path":"/docs/defaultdict/","result":{"data":{"mdx":{"fields":{"pageType":"doc","title":"collections.defaultdict","editLink":""},"body":"var _excluded = [\"components\"];\nfunction _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n/* @jsxRuntime classic */\n/* @jsx mdx */\n\nvar _frontmatter = {\n  \"title\": \"collections.defaultdict\",\n  \"section\": \"Quick Docs\",\n  \"order\": 7\n};\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n    props = _objectWithoutProperties(_ref, _excluded);\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"p\", null, \"The Python \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"defaultdict\"), \" type behaves almost exactly like a regular Python dictionary, but if you try to access or modify a missing key, the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"defaultdict\"), \" will automatically insert the key and generate a default value for it.\\nWhile instantiating a \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"defaultdict\"), \", we pass in a function that tells it how to create a default value for missing keys.\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-py\"\n  }, \">>> from collections import defaultdict\\n>>> my_dict = defaultdict(int)\\n>>> my_dict\\ndefaultdict(<class 'int'>, {})\\n\")), mdx(\"p\", null, \"In this example, we've used the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"int\"), \" class which returns 0 when called like a function, so any missing key will get a default value of 0. You can also get an empty list by default with \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"list\"), \" or an empty string with \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"str\"), \".\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-py\"\n  }, \">>> my_dict[\\\"foo\\\"]\\n0\\n>>> my_dict[\\\"bar\\\"] += 5\\n>>> my_dict\\ndefaultdict(<class 'int'>, {'foo': 0, 'bar': 5})\\n\")), mdx(\"p\", null, \"Check out the \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://docs.python.org/3/library/collections.html#collections.defaultdict\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"a\"\n  }, \"docs\")), \" to learn even more!\"));\n}\n;\nMDXContent.isMDXComponent = true;","tableOfContents":{}}},"pageContext":{"id":"807c9528-664e-5dfe-a645-bac2aed7baa8"}},"staticQueryHashes":["1022386572","1122327541","1339625561","2140385554","3442269102","3597609067"]}