-2

I keep getting this error when trying to reference a nested list. I know I must be within the actual range of the list, yet i still get the error

self = list()
        for index in points:
            if(index[0]==x or index[1]==y):
                self.append(index)

Above is the assignment to self. Points (which is what im referencing) is another nested list. This code works perfectly, but when i try to index the list as such :

self[0][0]

i get the following :

IndexError: list index out of range
    print(self[1][0])
Line 17 in nearestValidPoint (Solution.py)
    ret = Solution().nearestValidPoint(param_1, param_2, param_3)
Line 57 in _driver (Solution.py)
    _driver()
Line 68 in <module> (Solution.py)

When I try to print the original list points, it STILL throws me this error, IE:

print(points[1][0])

prints a single integer (which is expected) but the error is still thrown

X and Y are user inputted integers. The code I provided is the entire code

Majidf0
  • 11
  • 5
  • 3
    `self` is a keyword used in Python Classes and should always be avoided as a variable name. There is incorrect indenting in the code snippet you provided - why is for `index in points:` indented? More code detail is needed to see other problems causing errors. – user19077881 Oct 29 '22 at 21:02
  • 1
    @user19077881 It's ***not*** a keyword. And when you use it, you ***do*** use it as variable name. And nothing speaks against using it for something other than the instance in methods, if it's a good name for the value. – Kelly Bundy Oct 29 '22 at 21:26
  • By convention, `self` is used [as an instance in methods](https://stackoverflow.com/questions/4131582/why-is-self-only-a-convention-and-not-a-real-python-keyword) so it is confusing to see it used as a variable name. Other languages, such as Java, C++, C#, Javascript, etc., have `this` as a keyword that seems to serve a similar purpose as self, which would make it more confusing if `self` is used as an arbitrary variable. – DarrylG Oct 30 '22 at 00:04

1 Answers1

0

self (bad variable name) is probably no longer a nested list since you only append index.

Try print(points[1][0]) or print(self[0])

bitflip
  • 3,436
  • 1
  • 3
  • 22