chanwcom
chanwcom

Reputation: 4650

What is the difference between x[1, 1] and x.item(1, 1)?

In NumPy arrays, I have been always using brackets to specify an element of an array.

For example,

x = np.array([[0, 1], [2, 3]])

Then, x[1, 1] may be used to represent the item at the 1-st row and the 1-st column.

But today, I saw that some people are using x.item(1, 1) instead.

Are x.item(1, 1) and x[1, 1] exactly the same, or are there any differences?

Upvotes: 0

Views: 77

Answers (1)

Wasi Ahmad
Wasi Ahmad

Reputation: 37701

.item() copies an element of an array to a standard Python scalar and return it. But when you are using x[1, 1] - you reference a specific element from the nd-array.

Upvotes: 2

Related Questions