{"id":111758,"date":"2020-04-19T17:27:36","date_gmt":"2020-04-19T17:27:36","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=7675"},"modified":"2020-04-19T17:27:36","modified_gmt":"2020-04-19T17:27:36","slug":"resolve-indexerror-list-assignment-index-out-of-range","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/04\/19\/resolve-indexerror-list-assignment-index-out-of-range\/","title":{"rendered":"[Resolve] IndexError: List Assignment Index Out of Range"},"content":{"rendered":"<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"[Resolved] IndexError: List Assignment Index Out of Range\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/NgweMbsLqY8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p><strong>When does the <code>IndexError: list assignment index out of range<\/code> appear?<\/strong><\/p>\n<p><strong>Python throws an <code>IndexError<\/code> if you try to assign a value to a list index that doesn&#8217;t exist, yet. For example, if you execute the expression <code>list[1] = 10<\/code> on an empty <code>list<\/code>, Python throws the <code>IndexError<\/code>. Simply resolve it by adding elements to your list until the index actually exists.<\/strong><\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/image-2.png\" alt=\"\" class=\"wp-image-7676\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/image-2.png 899w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/image-2-300x73.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/image-2-768x186.png 768w\" sizes=\"(max-width: 899px) 100vw, 899px\" \/><\/figure>\n<p>Here&#8217;s the minimal example that throws the IndexError:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">lst = []\nlst[1] = 10<\/pre>\n<p>If you run this code, you&#8217;ll see that Python throws an <code>IndexError<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Traceback (most recent call last): File \"C:\\Users\\xcent\\Desktop\\code.py\", line 2, in &lt;module> lst[1] = 10\nIndexError: list assignment index out of range<\/pre>\n<p>You can resolve it by adding two &#8220;dummy&#8221; elements to the list so that the index 1 actually exists in the list:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">lst = [None, None]\nlst[1] = 10\nprint(lst)<\/pre>\n<p>Now, Python will print the expected output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[None, 10]<\/pre>\n<p>Try to fix the <code>IndexError<\/code> in the following interactive code shell:<\/p>\n<p> <iframe loading=\"lazy\" height=\"400px\" width=\"100%\" src=\"https:\/\/repl.it\/@finxter\/RemarkablePungentCensorware?lite=true\" scrolling=\"no\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\" sandbox=\"allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals\"><\/iframe> <\/p>\n<p><strong>Exercise<\/strong>: Can you fix this code?<\/p>\n<p>So what are some other occurrences of the IndexError?<\/p>\n<h2>IndexError in For Loop<\/h2>\n<p>Frequently, the <code>IndexError<\/code> happens if you use a <code>for<\/code> loop to modify some list elements like here:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># WRONG CODE:\nlst = []\nfor i in range(10): lst[i] = i\nprint(lst)<\/pre>\n<p>Again, the result is an <code>IndexError<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Traceback (most recent call last): File \"C:\\Users\\xcent\\Desktop\\code.py\", line 4, in &lt;module> lst[i] = i\nIndexError: list assignment index out of range<\/pre>\n<p>You modify a list element at index <code>i<\/code> that doesn&#8217;t exist in the list. Instead, create the list using the <code>list(range(10))<\/code> list constructor.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># CORRECT CODE:\nlst = list(range(10))\nprint(lst)\n# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<\/pre>\n<h2>Where to Go From Here?<\/h2>\n<p>You&#8217;ve learned how to resolve one error. By doing this, your Python skills have improved a little bit. Do this every day and soon, you&#8217;ll be a skilled master coder.<\/p>\n<p>Do you want to leverage those skills in the most effective way? In other words: <strong>do you want to earn money with Python?<\/strong><\/p>\n<p>If the answer is yes, let me show you a simple way how you can create your simple, home-based coding business online:<\/p>\n<p><strong><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">Join Free Webinar: How to Become a Six-Figure Coder as an Average Coder?<\/a><\/strong><\/p>\n<p>Start your new thriving coding business now!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When does the IndexError: list assignment index out of range appear? Python throws an IndexError if you try to assign a value to a list index that doesn&#8217;t exist, yet. For example, if you execute the expression list[1] = 10 on an empty list, Python throws the IndexError. Simply resolve it by adding elements to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-111758","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/111758","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=111758"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/111758\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=111758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=111758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=111758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}