What is the difference between grid[index] VS grid[index, :] in python

Question:

In this https://colab.research.google.com/drive/1gS2aJo711XJodqqPIVIbzgX1ktZzS8d8?usp=sharing , they used np.max(qtable[new_state, :])

But I did an experiment and I don’t understand the need of : . My experiement show the same value, same array shape

import numpy as np

N = 10
grid = np.array([[np.array(k) for i in range(N)] for k in range(N)])
print(grid)
index = 5
d = grid[index]
e = grid[index, :]
print(d)
print(e)
Asked By: TSR

||

Answers:

As you have noticed, qtable[new_state, :] and qtable[new_state] are indeed equivalent.

If I were writing that code myself, I would also use the : because I believe it makes the code more self-documenting, flagging to me and future readers of the code (maybe me in six months) that there is a second dimension and I’m grabbing all its values.

The colon also makes it explicit that this is a NumPy Array slice operation, and not a simple list element operation.

But it’s just a question of style and documentation. (I don’t know if there’s a performance impact, but I don’t expect there is.)

Answered By: joanis