Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] [Resolve] IndexError: List Assignment Index Out of Range

#1
[Resolve] IndexError: List Assignment Index Out of Range

<div><figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe 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>
</div>
</figure>
<p><strong>When does the <code>IndexError: list assignment index out of range</code> appear?</strong></p>
<p><strong>Python throws an <code>IndexError</code> if you try to assign a value to a list index that doesn’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>
<figure class="wp-block-image size-large"><img 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/uplo...300x73.png 300w, https://blog.finxter.com/wp-content/uplo...68x186.png 768w" sizes="(max-width: 899px) 100vw, 899px" /></figure>
<p>Here’s the minimal example that throws the IndexError:</p>
<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 = []
lst[1] = 10</pre>
<p>If you run this code, you’ll see that Python throws an <code>IndexError</code>:</p>
<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
IndexError: list assignment index out of range</pre>
<p>You can resolve it by adding two “dummy” elements to the list so that the index 1 actually exists in the list:</p>
<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]
lst[1] = 10
print(lst)</pre>
<p>Now, Python will print the expected output:</p>
<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>
<p>Try to fix the <code>IndexError</code> in the following interactive code shell:</p>
<p> <iframe 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>
<p><strong>Exercise</strong>: Can you fix this code?</p>
<p>So what are some other occurrences of the IndexError?</p>
<h2>IndexError in For Loop</h2>
<p>Frequently, the <code>IndexError</code> happens if you use a <code>for</code> loop to modify some list elements like here:</p>
<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:
lst = []
for i in range(10): lst[i] = i
print(lst)</pre>
<p>Again, the result is an <code>IndexError</code>:</p>
<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
IndexError: list assignment index out of range</pre>
<p>You modify a list element at index <code>i</code> that doesn’t exist in the list. Instead, create the list using the <code>list(range(10))</code> list constructor.</p>
<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:
lst = list(range(10))
print(lst)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</pre>
<h2>Where to Go From Here?</h2>
<p>You’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’ll be a skilled master coder.</p>
<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>
<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>
<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>
<p>Start your new thriving coding business now!</p>
</div>


https://www.sickgaming.net/blog/2020/04/...-of-range/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016