Is there a difference between `board[x, y]` and `board[x][y]` in Python?

I'm working through a tutorial on GeekforGeeks website and noticed that they are checking a point in an array using board[x,y], which I've never seen before. I don't think this would work, but when I run the program, everything goes as expected.

I tried running a smaller code example using their method outlined above vs the method I'm more familiar with (board[x][y]), but when I run my code, I get TypeError: list indices must be integers or slices, not tuple

My code:

board = [[1,1,1], [1,2,2], [1,2,2]]
win = 'True'

if board[1][1] == 2:
    win = 'True by normal standards'
    print(win)
if board[1, 1] == 2:
    win = 'True by weird standards'
    print(win)

print(win)

Their code:

def row_win(board, player): 
    for x in range(len(board)): 
        win = True

        for y in range(len(board)): 
            if board[x, y] != player: 
                win = False
                continue

        if win == True: 
            return(win) 
    return(win) 

Can someone explain to me why board[x,y] works, and what exactly is happening? I've never seen this before except to create lists, and am not grasping it conceptually.


Solution 1:

They're able to do that since they're using NumPy, which won't throw an error on that.

>>> a = np.array([[1,1,1], [1,2,2], [1,2,2]])
>>> a[1,1]
2
>>> # equivalent to
>>> a = [[1,1,1], [1,2,2], [1,2,2]]
>>> a[1][1]
2
>>> 

Solution 2:

That works because the object they are using (in this case numpy array) overloads the __getitem__ method. See this toy example:

class MyArray:
  def __init__(self, arr):
    self.arr = arr
  def __getitem__(self, t):
    return self.arr[t[0]][t[1]]

myarr = MyArray([[1,1,1], [1,2,2], [1,2,2]])
print(myarr[0,1])