-5
j=0
i=0
text=[[0 for x in range(5)]for y in range(2)]
while (i<5):
    for link in soup.findAll('td'):
        if j<2:
            text[i][j]=link.string
            j+=1
i+=1

The problem is I get the error message list index out of range but I already set the if condition so if j exceed 3 will nth happen. So what is the problem?

1
  • you get your i and j indices mixed up. Commented Jul 29, 2016 at 7:56

2 Answers 2

1
[[0 for x in range(5)] for y in range(2)] 

creates the array [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

Your code is built for an array which looks like: [[0, 0], [0, 0],[0, 0],[0, 0],[0, 0]].

So either i and j are around the wrong way or your:

[[0 for x in range(5)] for y in range(2)] 

isn't giving you what you expect.

Sign up to request clarification or add additional context in comments.

Comments

1
text=[[0 for x in range(5)]for y in range(2)]

is equal to

[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

So inside your list, you have 2 lists with 5 elements each one.

Then look at this line:

text[i][j]=link.string

i - is number of elements in variable text (you have 2)

j - is number of elements in each list (you have 5 elements)

It looks like you have mixed i and j.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.