user6912880
user6912880

Reputation: 29

List index out of range though appearing not to be

I wrote a simple program to check if strings are substrings of eachother. The issue is I keep getting a list index out of bounds error.

I tried printing i and j with each iteration and they never go out of the bounds of the list. I even tried to insert elements at s[5] and s[6] to check the index but still get the same error. What could be the cause of this error?

s = []                                        
s.insert(0,str("a b c"))
s.insert(1,str("a b c d"))
s.insert(2,str("a b"))
s.insert(3,str("b c"))
s.insert(4,str("d"))

j = 0
i = 0

while j < 5:
    if s[j] in s[i]:
        print("\"" + s[j] + "\" is in the string \"" + s[i] + "\"")
    i +=1
    if i == 5 and j < 4:
        j+=1
        i=0

This is my console output

Traceback (most recent call last):
"a b c" is in the string "a b c"
  File "C:/Users/Kal/PycharmProjects/untitled/FSS2.py", line 16, in <module>
"a b c" is in the string "a b c d"
    if s[j] in s[i]:
"a b c d" is in the string "a b c d"
IndexError: list index out of range
"a b" is in the string "a b c"
"a b" is in the string "a b c d"
"a b" is in the string "a b"
"b c" is in the string "a b c"
"b c" is in the string "a b c d"
"b c" is in the string "b c"
"d" is in the string "a b c d"
"d" is in the string "d"

Process finished with exit code 1

Upvotes: 0

Views: 751

Answers (5)

Nf4r
Nf4r

Reputation: 1410

The problem is in the line 18

s = []                                        
s.insert(0,str("a b c"))
s.insert(1,str("a b c d"))
s.insert(2,str("a b"))
s.insert(3,str("b c"))
s.insert(4,str("d"))
print(s)
j = 0
i = 0

while j < 5:
    if s[j] in s[i]:

        print("\"" + s[j] + "\" is in the string \"" + s[i] + "\"")
    i +=1
    if i == 5 and j < 4: <-- here
        j+=1
        i=0

At some point, your i = 5 and j = 4, so the right side of this if i == 5 and j < 4 statement is being False, and the i is not reseted to 0. So at the next loop, the i is equal to 5, and the maximum index is 4.

Better solution would be to use for loops.

s = []                                        
s.insert(0,str("a b c"))
s.insert(1,str("a b c d"))
s.insert(2,str("a b"))
s.insert(3,str("b c"))
s.insert(4,str("d"))
for i in range(len(s)):
   for j in range(len(s)):
       if s[i] in s[j]:
          print("\"" + s[i] + "\" is in the string \"" + s[j] + "\"")

Edit to answer comment

s = []                                        
s.insert(0,str("a b c"))
s.insert(1,str("a b c d"))
s.insert(2,str("a b"))
s.insert(3,str("b c"))
s.insert(4,str("d"))
j = 0
i = 0

while j < len(s):
    if s[j] in s[i]:
        print("\"" + s[j] + "\" is in the string \"" + s[i] + "\"")
    i +=1
    if i == len(s):
       j+=1
       i=0

Upvotes: 1

geo1230
geo1230

Reputation: 252

Keeping your form it would be:

s = []                                        
s.insert(0,str("a b c"))
s.insert(1,str("a b c d"))
s.insert(2,str("a b"))
s.insert(3,str("b c"))
s.insert(4,str("d"))

j = 0
i = 0

while j < 5:
    if s[j] in s[i]:
        print("\"" + s[j] + "\" is in the string \"" + s[i] + "\"")
    i +=1
    if i == 5 and j <= 4:
        j+=1
        i=0

The error is in the 2nd if in your while loop. It should be j <= 4 or j < 5 in order to work.

Upvotes: 0

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48090

At the point when your code is raising the exception, the value of i is 5 and the value of j is 4. In your print statement you try to do s[i] i.e. s[5]and since max index of s is 4, your code is raising IndexError.

I believe, in your code you need to do make modification in your if statement as:

if i == 5 and j < 5:  # Instead of j < 4

Then your code runs fine:

>>> while j < 5:
...     if s[j] in s[i]:
...         print("\"" + s[j] + "\" is in the string \"" + s[i] + "\"")
...     i +=1
...     if i == 5 and j < 5:
...         j+=1
...         i=0
... 
"a b c" is in the string "a b c"
"a b c" is in the string "a b c d"
"a b c d" is in the string "a b c d"
"a b" is in the string "a b c"
"a b" is in the string "a b c d"
"a b" is in the string "a b"
"b c" is in the string "a b c"
"b c" is in the string "a b c d"
"b c" is in the string "b c"
"d" is in the string "a b c d"
"d" is in the string "d"

Upvotes: 0

vLance
vLance

Reputation: 1

Your i and j variables in while loop are incorrect. After changing values following code is working.

s = []                                        
s.insert(0,str("a b c"))
s.insert(1,str("a b c d"))
s.insert(2,str("a b"))
s.insert(3,str("b c"))
s.insert(4,str("d"))

print s

j = 0
i = 0

while j < 5:
    if s[j] in s[i]:
        print("\"" + s[j] + "\" is in the string \"" + s[i] + "\"")
    i +=1
    if i == 4 and j < 5:
        j+=1
        i=0

Upvotes: 0

imant
imant

Reputation: 627

You seem to increase j only when i is already 5 (notice the and in the if-clause). Thus, when i=5 you are still in the while loop (which only depends on j) and you try to access s[i] = s[5] which is undefined.

Upvotes: 0

Related Questions