{"id":19896,"date":"2020-12-13T00:30:06","date_gmt":"2020-12-13T08:30:06","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=19896"},"modified":"2023-12-01T04:05:51","modified_gmt":"2023-12-01T12:05:51","slug":"python-keyerror","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-keyerror\/","title":{"rendered":"Python KeyError: A Beginner&#8217;s Guide"},"content":{"rendered":"\n<p><em>A Python KeyError is raised when you try to access an item in a dictionary that does not exist. You can fix this error by modifying your program to select an item from a dictionary that does exist. Or you can handle this error by checking if a key exists first.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Handle a Python KeyError<\/h2>\n\n\n\n<p>Have you just been greeted by a KeyError? Don\u2019t worry! When a KeyError is raised, it means that you are trying to access a key inside a dictionary that does not exist. Handing and fixing this error is easy when you know how.<\/p>\n\n\n\n<p>KeyErrors can be handled using a try\u2026except block, using the \u201cin\u201d keyword, or by checking for a key in advance using indexing.<\/p>\n\n\n\n<p>In this guide, we\u2019re going to talk about what Python KeyErrors are and why they are raised. We\u2019ll walk through an example of an error and how to fix it so you\u2019ll know how to address KeyErrors in the future.<\/p>\n\n\n\n<p>Let\u2019s get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Python KeyError?<\/h2>\n\n\n\n<p>A Python KeyError occurs when you attempt to access an item in a dictionary that does not exist using the indexing syntax. This error is raised because Python cannot return a value for an item that does not exist in a dictionary.<\/p>\n\n\n\n<p>A <a href=\"https:\/\/careerkarma.com\/blog\/python-dictionary\/\">Python dictionary<\/a> is a set of key-value pairs that are stored within curly braces ({}):<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>raspberry_pi = {\n&quot;name&quot;: &quot;Raspberry Pi 4&quot;,\n&quot;price&quot;: 35.00,\n&quot;RAM&quot;: &quot;4GB&quot;\n}<\/pre><\/div>\n\n\n\n<p>The keys are \u201cname\u201d, \u201cprice\u201d, and \u201cRAM\u201d. Keys appear before a colon in a dictionary.<\/p>\n\n\n\n<p>Let\u2019s see what happens if we try to retrieve the number of USB ports in our dictionary:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>print(raspberry_pi[&quot;usb_ports&quot;])<\/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>KeyError: 'usb_ports'<\/pre><\/div>\n\n\n\n<p>This error tells us the key we have specified does not exist. In this case, the missing key is \u201cusb_ports\u201d.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">KeyError Python: Solutions<\/h2>\n\n\n\n<p>To handle a Python dictionary KeyError you can:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Check for a key in advance using indexing<\/li><li>Use the \u201cin\u201d keyword to check for a key<\/li><li>Use a try\u2026except block.<\/li><\/ul>\n\n\n\n<p>The solution to handle the KeyError that works best for you will vary depending on your use case.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Use the Dictionary get() Method<\/h3>\n\n\n\n<p>The <a href=\"https:\/\/careerkarma.com\/blog\/python-dictionary-get\/\">Python dictionary get() method <\/a>returns a value in a dictionary. You can specify a default value with the get() method. This default value is returned if the name of the key you have specified does not have a value.<\/p>\n\n\n\n<p>Consider the following code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>raspberry_pi = { &quot;name&quot;: &quot;Raspberry Pi 4&quot;, &quot;price&quot;: 35.00, &quot;RAM&quot;: &quot;4GB&quot; }\n\nget_key = input(&quot;What information would you like to retrieve (name, price, RAM)? &quot;)\n\nprint(&quot;The {} for this computer is {}.&quot;.format(get_key, raspberry_pi.get(get_key, &quot;not available&quot;))<\/pre><\/div>\n\n\n\n<p>Let&#8217;s run our code using the Python interpreter and see what happens:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>What information would you like to retrieve? ALU\nThe ALU for this computer is not available.<\/pre><\/div>\n\n\n\n<p>Our code uses the dictionary get() method to retrieve &#8220;ALU&#8221; from our dictionary. No value corresponds with this key name. The get() method returns the default value we have specified, which is &#8220;not available&#8221;.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Check for a Key in Advance Using Indexing<\/h3>\n\n\n\n<p>You can check for the existence of a key using indexing to avoid encountering a KeyError. If a key is in a dictionary, you can use that key. Otherwise, you can instruct your program to do something else that does not depend on that key.\n\n<\/p>\n\n\n\n<p>Let\u2019s write a program that accesses keys in our \u201craspberry_pi\u201d dictionary in Python. Let\u2019s check if our key is in our dictionary using indexing:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>raspberry_pi = { &quot;name&quot;: &quot;Raspberry Pi 4&quot;, &quot;price&quot;: 35.00, &quot;RAM&quot;: &quot;4GB&quot; }\n\nget_key = input(&quot;What information would you like to retrieve (name, price, RAM)? &quot;)\n\nif raspberry_pi[get_key]:\nprint(&quot;The {} for this computer is {}.&quot;.format(get_key, raspberry_pi[get_key]))\nelse:\n\tprint(&quot;This information is not available.&quot;)<\/pre><\/div>\n\n\n\n<p>We have used a <a href=\"https:\/\/careerkarma.com\/blog\/python-if-else\/\">Python \u201cif\u201d statement<\/a> to check if our key exists. \u201craspberry_pi[get_key]\u201d will only return a value if the key exists. This means that if our key exists, our \u201cif\u201d statement will execute. If our key does not exist, the \u201celse\u201d statement will execute.\n\n<\/p>\n\n\n\n<p>Let\u2019s try to find out the RAM for our Raspberry Pi:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>What information would you like to retrieve (name, price, RAM)? RAM\nThe RAM for this computer is 4GB.<\/pre><\/div>\n\n\n\n<p>Now, let&#8217;s try to find out about how many USB ports the computer has:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>What information would you like to retrieve (name, price, RAM)? USB\nThis information is not available.<\/pre><\/div>\n\n\n\n<p>Our code does not raise a KeyError! This is because we\u2019ve checked if our key exists first before using it. If we had used raspberry_pi[get_key] inside our \u201celse\u201d statement, our code would return a KeyError.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using a try&#8230;except Block<\/h3>\n\n\n\n<p>A <a href=\"https:\/\/careerkarma.com\/blog\/python-try-except\/\">Python try&#8230;except block<\/a> will try to run a line of code. If that line of code cannot be run, our code will raise a custom error according to the exceptions you have stated.<\/p>\n\n\n\n<p>We can use a try\u2026except block to determine the existence of an error and stop an unexpected KeyError from helping our Python program.\n\n<\/p>\n\n\n\n<p>Let\u2019s define a custom error for our computer example from earlier using a try\u2026except block:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>raspberry_pi = { &quot;name&quot;: &quot;Raspberry Pi 4&quot;, &quot;price&quot;: 35.00, &quot;RAM&quot;: &quot;4GB&quot; }\n\nget_key = input(&quot;What information would you like to retrieve (name, price, RAM)? &quot;)\n\ntry:\n\tprint(&quot;The {} for this computer is {}.&quot;.format(get_key, raspberry_pi[get_key]))\nexcept KeyError:\n\tprint(&quot;This information is not available.&quot;)<\/pre><\/div>\n\n\n\n<p>We use a try\u2026except block to try to print out a particular piece of information about the Raspberry Pi computer to the console. If our code raises a KeyError anywhere in the &#8220;try&#8221; block, our code executes the contents of the \u201cexcept\u201d block.\n\n<\/p>\n\n\n\n<p>This means that every time a KeyError is raised, \u201cThis information is not available\u201d is printed to the console. Let\u2019s try to find out about the computer\u2019s name:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>What information would you like to retrieve (name, price, RAM)? name\nThe name for this computer is Raspberry Pi 4.<\/pre><\/div>\n\n\n\n<p>Now, let&#8217;s try to learn about the computer&#8217;s USB port availability:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>What information would you like to retrieve (name, price, RAM)? USB\nThis information is not available.<\/pre><\/div>\n\n\n\n<p>Our code raises a KeyError inside our \u201ctry\u201d block. This causes our code to execute the \u201cexcept\u201d block of code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Check for a Key in Advance Using in<\/h3>\n\n\n\n<p>The \u201cin\u201d keyword is one of Python\u2019s membership operators. It checks if an item is in a list of values. You can use the \u201cin\u201d keyword to check if a key is inside a dictionary.\n\n<\/p>\n\n\n\n<p>This program will check if a key exists before printing out its value to the console:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>raspberry_pi = { &quot;name&quot;: &quot;Raspberry Pi 4&quot;, &quot;price&quot;: 35.00, &quot;RAM&quot;: &quot;4GB&quot; }\n\nget_key = input(&quot;What information would you like to retrieve (name, price, RAM)? &quot;)\n\nif get_key in raspberry_pi:\nprint(&quot;The {} for this computer is {}.&quot;.format(get_key, raspberry_pi[get_key]))\nelse:\n\tprint(&quot;This information is not available.&quot;)<\/pre><\/div>\n\n\n\n<p>This code will check if the value of \u201cget_key\u201d is a key inside the \u201craspberry_pi\u201d dictionary. If the key exists, the \u201cif\u201d statement will run. Otherwise, the \u201celse\u201d statement will run. Let\u2019s try to check for information about the computer\u2019s CPU:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>What information would you like to retrieve (name, price, RAM)? CPU\nThis information is not available.<\/pre><\/div>\n\n\n\n<p>Let&#8217;s check for the name of the computer:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>What information would you like to retrieve (name, price, RAM)? name\nThe name for this computer is Raspberry Pi 4.<\/pre><\/div>\n\n\n\n<p>Our code works! When we search for a key that does not exist, the &#8220;else&#8221; statement runs. This means that no KeyError can be raised in our code because we do not attempt to access a key in the &#8220;else&#8221; block.<\/p>\n\n\n\n<p>Our code works! When we search for a key that does not exist, the \u201celse\u201d statement runs. This means that no KeyError can be raised in our code because we do not attempt to access a key in the \u201celse\u201d block.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>A KeyError is raised when you try to access a value from a dictionary that does not exist.  To solve a key error, you can check for a key upfront before using it and only use it if the key exists. You can use a try\u2026except block to handle a key error.<\/p>\n\n\n\n<p>For advice on how to learn the Python programming language, check out our comprehensive <a href=\"https:\/\/careerkarma.com\/blog\/how-to-learn-python\/\">How to Learn Python guide<\/a>. You&#8217;ll find links to top learning resources, books, and courses in this guide.<\/p>\n","protected":false},"excerpt":{"rendered":"A Python KeyError is raised when you try to access an item in a dictionary that does not exist. You can fix this error by modifying your program to select an item from a dictionary that does exist. Or you can handle this error by checking if a key exists first. How to Handle a&hellip;","protected":false},"author":240,"featured_media":19897,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-19896","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":null,"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 KeyError: A Beginner&#039;s Guide %<\/title>\n<meta name=\"description\" content=\"A Python KeyError is raised when you try to access a dictionary key that does not exist. On Career Karma, learn how to handle a Python KeyError.\" \/>\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-keyerror\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python KeyError: A Beginner&#039;s Guide\" \/>\n<meta property=\"og:description\" content=\"A Python KeyError is raised when you try to access a dictionary key that does not exist. On Career Karma, learn how to handle a Python KeyError.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-keyerror\/\" \/>\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-12-13T08:30:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T12:05:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/python-book-1181671.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=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-keyerror\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-keyerror\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Python KeyError: A Beginner&#8217;s Guide\",\"datePublished\":\"2020-12-13T08:30:06+00:00\",\"dateModified\":\"2023-12-01T12:05:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-keyerror\/\"},\"wordCount\":1109,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-keyerror\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/python-book-1181671.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-keyerror\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-keyerror\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/python-keyerror\/\",\"name\":\"Python KeyError: A Beginner's Guide %\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-keyerror\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-keyerror\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/python-book-1181671.jpg\",\"datePublished\":\"2020-12-13T08:30:06+00:00\",\"dateModified\":\"2023-12-01T12:05:51+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"A Python KeyError is raised when you try to access a dictionary key that does not exist. On Career Karma, learn how to handle a Python KeyError.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-keyerror\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-keyerror\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-keyerror\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/python-book-1181671.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/python-book-1181671.jpg\",\"width\":1020,\"height\":681},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-keyerror\/#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 KeyError: A Beginner&#8217;s 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 KeyError: A Beginner's Guide %","description":"A Python KeyError is raised when you try to access a dictionary key that does not exist. On Career Karma, learn how to handle a Python KeyError.","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-keyerror\/","og_locale":"en_US","og_type":"article","og_title":"Python KeyError: A Beginner's Guide","og_description":"A Python KeyError is raised when you try to access a dictionary key that does not exist. On Career Karma, learn how to handle a Python KeyError.","og_url":"https:\/\/careerkarma.com\/blog\/python-keyerror\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-12-13T08:30:06+00:00","article_modified_time":"2023-12-01T12:05:51+00:00","og_image":[{"width":1020,"height":681,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/python-book-1181671.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":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-keyerror\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-keyerror\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Python KeyError: A Beginner&#8217;s Guide","datePublished":"2020-12-13T08:30:06+00:00","dateModified":"2023-12-01T12:05:51+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-keyerror\/"},"wordCount":1109,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-keyerror\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/python-book-1181671.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-keyerror\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-keyerror\/","url":"https:\/\/careerkarma.com\/blog\/python-keyerror\/","name":"Python KeyError: A Beginner's Guide %","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-keyerror\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-keyerror\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/python-book-1181671.jpg","datePublished":"2020-12-13T08:30:06+00:00","dateModified":"2023-12-01T12:05:51+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"A Python KeyError is raised when you try to access a dictionary key that does not exist. On Career Karma, learn how to handle a Python KeyError.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-keyerror\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-keyerror\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-keyerror\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/python-book-1181671.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/python-book-1181671.jpg","width":1020,"height":681},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-keyerror\/#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 KeyError: A Beginner&#8217;s 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\/19896","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=19896"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19896\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/19897"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=19896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=19896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=19896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}