{"componentChunkName":"component---node-modules-smooth-doc-src-templates-doc-js","path":"/docs/dict-get/","result":{"data":{"mdx":{"fields":{"pageType":"doc","title":"dict.get","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\": \"dict.get\",\n  \"section\": \"Quick Docs\",\n  \"order\": 8\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, \"Often while using dictionaries in Python, you may run into \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"KeyErrors\"), \". This error is raised when you try to access a key that isn't present in your dictionary. Python gives you some neat ways to handle them.\"), mdx(\"p\", null, \"The \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://docs.python.org/3/library/stdtypes.html#dict.get\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"a\"\n  }, \"dict.get\")), \" method will return the value for the key if it exists, and None (or a default value that you specify) if the key doesn't exist. Hence it will \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"never raise\"), \" a KeyError.\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-py\"\n  }, \">>> my_dict = {\\\"foo\\\": 1, \\\"bar\\\": 2}\\n>>> print(my_dict.get(\\\"foobar\\\"))\\nNone\\n\")), mdx(\"p\", null, \"Below, 3 is the default value to be returned, because the key doesn't exist-\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-py\"\n  }, \">>> print(my_dict.get(\\\"foobar\\\", 3))\\n3\\n\")), mdx(\"p\", null, \"Some other methods for handling \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"KeyError\"), \"s gracefully are the \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://docs.python.org/3/library/stdtypes.html#dict.setdefault\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"a\"\n  }, \"dict.setdefault\")), \" method and \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://docs.python.org/3/library/collections.html#collections.defaultdict\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"a\"\n  }, \"collections.defaultdict\")), \" (check out the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"!defaultdict\"), \" tag).\"));\n}\n;\nMDXContent.isMDXComponent = true;","tableOfContents":{}}},"pageContext":{"id":"a6304916-745a-5339-9a0d-7d2a645e4e3d"}},"staticQueryHashes":["1022386572","1122327541","1339625561","2140385554","3442269102","3597609067"]}