{"id":899,"date":"2024-02-23T13:21:13","date_gmt":"2024-02-23T13:21:13","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=899"},"modified":"2024-02-23T13:21:14","modified_gmt":"2024-02-23T13:21:14","slug":"numpy-array-iterating","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/02\/23\/numpy-array-iterating\/","title":{"rendered":"NumPy\u00a0Array Iterating"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Iterating Arrays<\/h2>\n\n\n\n<p>Iterating means going through elements one by one.<\/p>\n\n\n\n<p>As we deal with multi-dimensional arrays in numpy, we can do this using basic&nbsp;<code>for<\/code>&nbsp;loop of python.<\/p>\n\n\n\n<p>If we iterate on a 1-D array it will go through each element one by one.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<a href=\"https:\/\/www.w3schools.com\/python\/python_server.asp\" target=\"_blank\" rel=\"noreferrer noopener\">Get your own Python Server<\/a><\/h3>\n\n\n\n<p>Iterate on the elements of the following 1-D array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array(&#91;1,&nbsp;2,&nbsp;3])<br><br>for&nbsp;x&nbsp;in&nbsp;arr:<br>&nbsp;&nbsp;print(x)<\/code><\/pre>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_iterating1\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Iterating 2-D Arrays<\/h2>\n\n\n\n<p>In a 2-D array it will go through all the rows.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Iterate on the elements of the following 2-D array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0numpy\u00a0as\u00a0np<br><br>arr = np.array(&#91;&#91;1,\u00a02,\u00a03], &#91;4,\u00a05,\u00a06]])<br><br>for\u00a0x\u00a0in\u00a0arr:<br>\u00a0\u00a0print(x)<\/code><\/pre>\n\n\n\n<p>If we iterate on a&nbsp;<em>n<\/em>-D array it will go through n-1th dimension one by one.<\/p>\n\n\n\n<p>To return the actual values, the scalars, we have to iterate the arrays in each dimension.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Iterate on each scalar element of the 2-D array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array(&#91;&#91;1,&nbsp;2,&nbsp;3], &#91;4,&nbsp;5,&nbsp;6]])<br><br>for&nbsp;x&nbsp;in&nbsp;arr:<br>&nbsp;&nbsp;for&nbsp;y&nbsp;in&nbsp;x:<br>&nbsp;&nbsp;&nbsp;&nbsp;print(y)<\/code><\/pre>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_iterating2-2\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Iterating 3-D Arrays<\/h2>\n\n\n\n<p>In a 3-D array it will go through all the 2-D arrays.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Iterate on the elements of the following 3-D array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array(&#91;&#91;&#91;1,&nbsp;2,&nbsp;3], &#91;4,&nbsp;5,&nbsp;6]], &#91;&#91;7,&nbsp;8,&nbsp;9],&nbsp;&#91;10,&nbsp;11,&nbsp;12]]])<br><br>for&nbsp;x&nbsp;in&nbsp;arr:<br>&nbsp;&nbsp;print(x)<\/code><\/pre>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_iterating3\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<p>To return the actual values, the scalars, we have to iterate the arrays in each dimension.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Iterate down to the scalars:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array(&#91;&#91;&#91;1,&nbsp;2,&nbsp;3], &#91;4,&nbsp;5,&nbsp;6]], &#91;&#91;7,&nbsp;8,&nbsp;9],&nbsp;&#91;10,&nbsp;11,&nbsp;12]]])<br><br>for&nbsp;x&nbsp;in&nbsp;arr:<br>&nbsp;&nbsp;for&nbsp;y&nbsp;in&nbsp;x:<br>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;z&nbsp;in&nbsp;y:<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;print(z)<\/code><\/pre>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_iterating3-2\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Iterating Arrays Using nditer()<\/h2>\n\n\n\n<p>The function&nbsp;<code>nditer()<\/code>&nbsp;is a helping function that can be used from very basic to very advanced iterations. It solves some basic issues which we face in iteration, lets go through it with examples.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Iterating on Each Scalar Element<\/h3>\n\n\n\n<p>In basic&nbsp;<code>for<\/code>&nbsp;loops, iterating through each scalar of an array we need to use&nbsp;<em>n<\/em>&nbsp;<code>for<\/code>&nbsp;loops which can be difficult to write for arrays with very high dimensionality.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Iterate through the following 3-D array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array(&#91;&#91;&#91;1,&nbsp;2], &#91;3,&nbsp;4]], &#91;&#91;5,&nbsp;6], &#91;7,&nbsp;8]]])<br><br>for&nbsp;x&nbsp;in&nbsp;np.nditer(arr):<br>&nbsp;&nbsp;print(x)<\/code><\/pre>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_iterating4\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Iterating Array With Different Data Types<\/h2>\n\n\n\n<p>We can use&nbsp;<code>op_dtypes<\/code>&nbsp;argument and pass it the expected datatype to change the datatype of elements while iterating.<\/p>\n\n\n\n<p>NumPy does not change the data type of the element in-place (where the element is in array) so it needs some other space to perform this action, that extra space is called buffer, and in order to enable it in&nbsp;<code>nditer()<\/code>&nbsp;we pass&nbsp;<code>flags=['buffered']<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Iterate through the array as a string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array(&#91;1,&nbsp;2,&nbsp;3])<br><br>for&nbsp;x&nbsp;in&nbsp;np.nditer(arr, flags=&#91;'buffered'], op_dtypes=&#91;'S']):<br>&nbsp;&nbsp;print(x)<\/code><\/pre>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_iterating5\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Iterating With Different Step Size<\/h2>\n\n\n\n<p>We can use filtering and followed by iteration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Iterate through every scalar element of the 2D array skipping 1 element:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0numpy\u00a0as\u00a0np<br><br>arr = np.array(&#91;&#91;1,\u00a02,\u00a03,\u00a04], &#91;5,\u00a06,\u00a07,\u00a08]])<br><br>for\u00a0x\u00a0in\u00a0np.nditer(arr&#91;:, ::2]):<br>\u00a0\u00a0print(x)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Enumerated Iteration Using ndenumerate()<\/h2>\n\n\n\n<p>Enumeration means mentioning sequence number of somethings one by one.<\/p>\n\n\n\n<p>Sometimes we require corresponding index of the element while iterating, the&nbsp;<code>ndenumerate()<\/code>&nbsp;method can be used for those usecases.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Enumerate on following 1D arrays elements:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0numpy\u00a0as\u00a0np\n\narr = np.array(&#91;1,\u00a02,\u00a03])\n\nfor\u00a0idx, x\u00a0in\u00a0np.ndenumerate(arr):\n\u00a0\u00a0print(idx, x)\n\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Enumerate on following 2D array&#8217;s elements:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0numpy\u00a0as\u00a0np<br><br>arr = np.array(&#91;&#91;1,\u00a02,\u00a03,\u00a04], &#91;5,\u00a06,\u00a07,\u00a08]])<br><br>for\u00a0idx, x\u00a0in\u00a0np.ndenumerate(arr):<br>\u00a0\u00a0print(idx, x)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Iterating Arrays Iterating means going through elements one by one. As we deal with multi-dimensional arrays in numpy, we can do this using basic&nbsp;for&nbsp;loop of python. If we iterate on a 1-D array it will go through each element one by one. ExampleGet your own Python Server Iterate on the elements of the following 1-D [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-899","post","type-post","status-publish","format-standard","hentry","category-numpy"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/899","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=899"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/899\/revisions"}],"predecessor-version":[{"id":900,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/899\/revisions\/900"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}