{"id":9184,"date":"2021-03-26T16:28:20","date_gmt":"2021-03-26T10:58:20","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=9184"},"modified":"2022-09-11T13:38:08","modified_gmt":"2022-09-11T08:08:08","slug":"numpy-array-elements-slicing-and-indexing","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/numpy-array-elements-slicing-and-indexing\/","title":{"rendered":"Accessing elements of a Numpy Array through Slicing and Indexing"},"content":{"rendered":"\n<p>Just like in a list, element(s) of an array can be accessed through slicing and indexing. And it&#8217;s virtually the same process as in a list. In this tutorial, we will discuss how to index and array and go further to discuss array slicing. Let\u2019s begin with array indexing.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing a Single Element in an Array through Slicing&nbsp;<\/h2>\n\n\n\n<p>To slice an array, the index of the value to be accessed is specified in square brackets. Remember that when counting the index from the front, the counting begins from zero in Python. Thus, the first element has an index of 0, the second element has an index 1, and so on. See the example below.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary library\nimport numpy as np\n&nbsp;\narray1 = np.array([2, 4, 6, 8, 10])\n&nbsp;\nprint(array1[0])\n<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n2\n<\/code><\/pre>\n\n\n\n<p>The first element is printed.&nbsp;<\/p>\n\n\n\n<p>To print the 5th element, index 4 is specified in square brackets.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(array1[4])<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n10\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Counting the Array from the last Element<\/h2>\n\n\n\n<p>You can also count from behind. To index from the last element of the array, negative indices are used. The last element has an index of -1, the second to the last has an index of -2, and so on. See the example below.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary library\nimport numpy as np\n&nbsp;\narray1 = np.array([2, 4, 6, 8, 10])\n&nbsp;\n#accessing the last element in the list\nprint(array1[-1])<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n10\n<\/code><\/pre>\n\n\n\n<p>The last element is expected.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(array1[-2])<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n8\n<\/code><\/pre>\n\n\n\n<p>You can also access an element in a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Array_data_type#Multi-dimensional_arrays\" class=\"rank-math-link\" rel=\"nofollow noopener\" target=\"_blank\">multidimensional array<\/a>. The indices of the element to be accessed are separated by a comma and specified in square brackets.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary library\nimport numpy as np\n&nbsp;\narray1 = np.array([[12, 3, 9],&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[25, 0, 13],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[5, 8, 10]])\n&nbsp;\nprint(array1[1, 2])\n<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\nOutput:\n13\n<\/code><\/pre>\n\n\n\n<p>In the above example, we accessed the element in the 2nd row and 3rd column.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing Sub Arrays from a Numpy Array through Slicing&nbsp;<\/h2>\n\n\n\n<p>You can access subarrays in an array using the slicing notation (:). As with slicing a <a href=\"https:\/\/www.h2kinfosys.com\/blog\/python-lists-and-matrix-with-numpy\/\" class=\"rank-math-link\">list in Python<\/a>, the slice of a Numpy array is done using the syntax below.&nbsp;<\/p>\n\n\n\n<p>array[start:stop:step]<\/p>\n\n\n\n<p>By default, the step=1, the start=0, and the stop= size of the array. So if you wish to return the same array, you can write array[::] or better still array[:].&nbsp;<\/p>\n\n\n\n<p>Let&#8217;s see some coding examples.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Defining only the start index<\/h2>\n\n\n\n<p>When only the start index is defined, the program returns the elements from the start index to the end of the array<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary library\nimport numpy as np\n&nbsp;\narray1 = np.array([2, 4, 6, 8, 10])\n&nbsp;\nprint(array1[1:])\n<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n&#91; 4  6  8 10]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Define the start and stop index<\/h2>\n\n\n\n<p>When the stop index is specified, the program does stop at the element just before the stop index. It does not return the end index.&nbsp;&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary library\nimport numpy as np\n&nbsp;\narray1 = np.array([2, 4, 6, 8, 10])\n&nbsp;\nprint(array1[1:3])<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n&#91;4 6]\n<\/code><\/pre>\n\n\n\n<p>Notice that the element in index 3, (8) was not included in the output. This is critical to understand. The Python interpreter does not include the stop index during slicing.&nbsp;<\/p>\n\n\n\n<p>You can also specify the step.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary library\nimport numpy as np\n&nbsp;\narray1 = np.array([2, 4, 6, 8, 10])\n&nbsp;\nprint(array1[::2])<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n&#91; 2  6 10]\n<\/code><\/pre>\n\n\n\n<p>As seen, the program prints elements after every 2 steps.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Slicing Multidimensional Arrays&nbsp;<\/h2>\n\n\n\n<p>You can access the subarrays from an array through slicing as well.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary library\nimport numpy as np\n&nbsp;\narray1 = np.array([[2, 4, 6, 8, 10],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[12, 54, 24, 77, 34],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[1, 32, 78, -2, 87],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[0, 15, 29, 71, 88]])\n&nbsp;\nprint(array1[1:3, :3])<\/pre>\n\n\n\n<p>In the code above, the intersection of the second to the third row and the first 3 columns will be accessed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n&#91;&#91;12 54 24]\n &#91; 1 32 78]]\n<\/code><\/pre>\n\n\n\n<p>As expected.&nbsp;<\/p>\n\n\n\n<p>You can play around with the index to get different results. You can refer to these articles to learn more about <a class=\"rank-math-link\" href=\"https:\/\/www.h2kinfosys.com\/blog\/python-lists-and-matrix-with-numpy\/\">Python lists<\/a> and <a class=\"rank-math-link\" href=\"https:\/\/www.h2kinfosys.com\/blog\/python-arrays-create-reverse-remove-element\/\">Python arrays<\/a>. If you have any questions, feel free to leave them in the comment section and I\u2019d do my best to answer them.&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just like in a list, element(s) of an array can be accessed through slicing and indexing. And it&#8217;s virtually the same process as in a list. In this tutorial, we will discuss how to index and array and go further to discuss array slicing. Let\u2019s begin with array indexing.&nbsp; Accessing a Single Element in an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9188,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[498],"tags":[],"class_list":["post-9184","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-artificial-intelligence-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/9184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=9184"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/9184\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/9188"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=9184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=9184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=9184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}