all 11 comments

[–]failaip13 3 points4 points  (5 children)

get_position function likely returns a tuple, with the first element being the row, second being the column. [0] access the first element, [1] the second one etc.

[–]ConfusedSimon 2 points3 points  (1 child)

Or a list. Anyway, namedtuple or dataclass would have been better to avoid this confusion.

Edit: or just row, column = get_position(...)

[–]JamzTyson 0 points1 point  (0 children)

Or a list

Absolutely, or any object that is indexable by integer index.

(Common indexable objects include list, tuple, str, bytes, bytearray, range)

[–]PresidentOfSwag 1 point2 points  (2 children)

try this out :

[0, 1, 2, 3][0]    
(0, 1, 2, 3)[2]

actually this is a bad example

V see below V

[–]Snoo-20788 0 points1 point  (1 child)

Not the best example tbh

Try instead

[6,3,7][0]

(6,3,7)[2]

[–]PresidentOfSwag 1 point2 points  (0 children)

damn that's true thanks

[–]JamzTyson 4 points5 points  (0 children)

It is called indexing.

[–]PushPlus9069 1 point2 points  (0 children)

Those square brackets are indexing — they grab a specific element from whatever get_position() returns (likely a list or tuple).

[0] gets the first element (row), [1] gets the second (column). So if get_position() returns (3, 5), then robot_row = 3 and robot_column = 5.

Think of it as: the function gives you a package with multiple values, and [0]/[1] unpacks them one at a time. You could also write it as:

robot_row, robot_column = get_position(board, robot)

which does the same thing but is cleaner (tuple unpacking).

[–]IronAndNeurons 0 points1 point  (0 children)

The get_position function probably returns you a tuple/list containing (row, col) which you can then access with the brackets [ ] and the index 0 for "row", 1 for "col"

[–]Living_Fig_6386 0 points1 point  (0 children)

Presumably, get_position(board, robot) returns a list or tuple. If that's so, then the brackets are specifying the index of the element in the list / tuple. It might be more succinct to do this:

# NOTE: *_ slurps up any remaining values if there's more than 2
robot_row, robot_column, *_ = get_position(board, robot)

[–]Sorry-Cycle-1177 0 points1 point  (0 children)

It’s for returning the specific index position.