search
Search
Unlock 100+ guides
search toc
close
Outline
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to

Accessing a value in a NumPy 2D array

schedule Aug 12, 2023
Last updated
local_offer
PythonNumPy
Tags
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

To access a value in a Numpy 2D array, use the `[]` syntax.

Suppose we have the following 2D Numpy array:

``` x = np.array([[1,2,3], [4,5,6]])x array([[1, 2, 3], [4, 5, 6]]) ```

Accessing a value

To access the value residing at row 0 column 1 (i.e. the value 2):

``` x[0,1] 2 ```

To access the value residing at row 1 column 2 (i.e. the value 6):

``` x[1,2] 6 ```
WARNING

Do not use `[][]` syntax

In this previous code snippet, we could have used `x[1][2]` instead of `x[1,2]`, which would also return the value 6. However, it is not good practice to use double `[]`s in Numpy. `x[1][2]` involves two steps: firstly accessing row 1 (i.e. `[4,5,6]`), and then accessing the 2nd index (i.e. 6). On the other hand, `x[1,2]` involves just one step, that is, the value is directly fetched, and is therefore faster than double `[]`s.

Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
3
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!