Does "Indexerror: List Index Out of Range" When Trying to Access the N'Th Item Mean That My List Has Less Than N Items

Does IndexError: list index out of range when trying to access the N'th item mean that my list has less than N items?

If you have a list with 53 items, the last one is thelist[52] because indexing starts at 0.


From Real Python: Understanding the Python Traceback - IndexError:

IndexError

The IndexError is raised when you attempt to retrieve an index from a sequence, like a list or a tuple, and the index isn’t found in the sequence. The Python documentation defines when this exception is raised:

Raised when a sequence subscript is out of range. (Source)

Here’s an example that raises the IndexError:

test = list(range(53))
test[53]

---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-6-7879607f7f36> in <module>
1 test = list(range(53))
----> 2 test[53]

IndexError: list index out of range

The error message line for an IndexError doesn’t give you great information. You can see that you have a sequence reference that is out of range and what the type of the sequence is, a list in this case. That information, combined with the rest of the traceback, is usually enough to help you quickly identify how to fix the issue.

Why is the list index out of range in this case?

You are creating the following matrix:

[[-1, -1, -1, -1],
[-1, -1, -1, -1],
[-1, -1, -1, -1],
[-1, -1, -1, -1],
[-1, -1, -1, -1]]

So when you try to iterate through cols+1 (6 in this case) you overflowing the four columns total that you have created.

Plus, if you want to create a 3*4 matrix, why do you increase these:

rows,cols=(N+1,sum1+1)

In any case, you want cols-1 and rows-1. But as mentioned by @Rah Maha below, rows+1 works, so you are inverting the axis.

N = 3
sum1 = 4
rows, cols = (N+1, sum1+1) # still should not be +1 if you want a 3x4 matrix
# changed cols and rows position so it will be what you want
dp = [[-1 for i in range(cols)] for j in range(rows)]
for i in range(0, cols):
dp[0][i] = False

for i in range(0, rows):
dp[i][0] = True

Line 7, IndexError: list index out of range

First of all check page you try to parse. you wrote:

r"watch?v=(\S{11})"

just remember that ? char here will be parsed as REGEX operator and not string you want,
so first of all you need to write it like:

/watch[?]v=(\S{11})

so your regex will be parsed properly

Second: good practice to print your list to see what you get and iterate via list using FOR loop instead of directly accessing index [0].

in you case you get this error just because your list of id is empty.

next code is working for me

import urllib.request
import re

search_keyword="ill%20wiat"
url="https://www.youtube.com/results?search_query="+search_keyword
with urllib.request.urlopen(url) as response:
video_ids = re.findall("/watch[?]v=(\S{11})", response.read().decode())
for video in video_ids:
print("https://www.youtube.com/watch?v=" + video)

P.S don't wrap your code with try/except to catch such thrown errors

IndexError: list index out of range When creating an automated response bot

answer=data['A'].tolist()

and then later on

id1=val.argsort()[0][-2]
response = response+answer[id1]

So if the anwser don't have id1 in it you will get index out of range. So in your case the len(answer) >= id1 is true.



Related Topics



Leave a reply



Submit