'List index out of range Error in python ,but index is in range how is it?

def longestValidParentheses(si):
    cnt=0
    s=[]
    for _ in range(0,len(si)):
        s.append(si[_])
    print(s[6])
    for i in range(0,len(s)-1):
     print(i)
     if (s[i]=="("):
      for j in range(0,len(s)):
        if (s[j]==")"):
          cnt+=2
          s.pop(j)
          break
    print(cnt)   
    return cnt

longestValidParentheses(")(())()")

This my code to find how many correctly formed parenthesis but in the second loop I AM getting an error like this

Traceback (most recent call last): File "F:/yy.py", line 17, in longestValidParentheses(")(())()") File "F:/yy.py", line 9, in longestValidParentheses if (s[i]=="("): IndexError: list index out of range

but list is not out of range



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source