{"id":4595,"date":"2020-03-30T19:17:28","date_gmt":"2020-03-30T19:17:28","guid":{"rendered":"https:\/\/www.askpython.com\/?p=4595"},"modified":"2023-02-12T11:30:57","modified_gmt":"2023-02-12T11:30:57","slug":"array-slicing-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/array\/array-slicing-in-python","title":{"rendered":"Understanding Array Slicing in Python"},"content":{"rendered":"\n<p><strong>Array slicing in Python<\/strong> is a technique in programming that allows you to extract a portion of an array, or a sequence of elements in Python. This technique plays a crucial role in many data manipulation and analysis tasks, and is widely used in various applications ranging from scientific computing, web development, and data analysis. <\/p>\n\n\n\n<p>In this article, you will learn the fundamentals of array slicing in Python, including syntax, examples, and best practices for using this powerful feature in your code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Array Slicing in Python?<\/h2>\n\n\n\n<p><strong>Array slicing in Python<\/strong> is a powerful feature that allows you to create new sub-arrays from existing arrays by specifying the starting and ending indices. The new sub-array is a portion of the original array, and it can be used for a variety of purposes, such as processing specific data, managing memory usage, and more.<\/p>\n\n\n\n<p>Array slicing can be easily done following the Python slicing method. For which the syntax is given below. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\narr&#x5B; start : stop : step ]\n<\/pre><\/div>\n\n\n<p>Again, Python also provides a function named <a href=\"https:\/\/www.askpython.com\/python\/built-in-methods\/python-slice-function\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">slice()<\/a> which returns a <strong>slice<\/strong> object containing the indices to be sliced. The syntax for using this method is given below. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nslice(start, stop&#x5B;, step])\n<\/pre><\/div>\n\n\n<p>For both the cases,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>start<\/strong> is the starting index from which we need to slice the array arr. By default set to 0,<\/li>\n\n\n\n<li><strong>stop<\/strong> is the ending index, before which the slicing operation would end. By default equal to the length of the array, <\/li>\n\n\n\n<li><strong>step<\/strong> is the steps the slicing process would take from start to stop. By default set to 1.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Methods for Array Slicing in Python<\/h2>\n\n\n\n<p>So now that we know the syntax for using both the methods, let us look at some examples and try to understand the <strong>slicing procedure<\/strong>. <\/p>\n\n\n\n<p>In the following examples, we are going to consider both arrays from the array module as well as <a aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-arrays\" target=\"_blank\" class=\"rank-math-link\">NumPy arrays<\/a>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. With one parameter<\/h2>\n\n\n\n<p>Default values for <strong>start<\/strong>, <strong>stop<\/strong> and <strong>step<\/strong> are equal to 0, length of the array, and 1 respectively. Hence, specifying either one of the start or stop, we can slice an array. <\/p>\n\n\n\n<p>Let us see how. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport array\nimport numpy as np\n\n#array initialisation\narray_arr= array.array(&#039;i&#039;,&#x5B;1,2,3,4,5])\nnp_arr = np.array(&#x5B;6,7,8,9,10])\n\n#slicing array with 1 parameter\nprint(&quot;Sliced array: &quot;, array_arr&#x5B;:3])\nprint(&quot;Sliced NumPy array: &quot;, np_arr&#x5B;:4])\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nSliced array:  array(&#039;i&#039;, &#x5B;1, 2, 3])\nSliced NumPy array:  &#x5B;6 7 8 9]\n<\/pre><\/div>\n\n\n<p>Here, we have initialized two arrays one from <code>array<\/code> module and another <code>NumPy<\/code> array. Slicing both of them using one parameter results are shown in the output. As we can see for both the cases, <strong>start<\/strong> and <strong>step<\/strong> are set by default to <strong>0<\/strong> and <strong>1<\/strong>. The sliced arrays contain elements of indices <strong>0<\/strong> to <strong>(stop-1)<\/strong>. This is one of the quickest methods of array slicing in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Array Slicing in Python With two parameters<\/h2>\n\n\n\n<p>Again, specifying any two parameters among the start, stop and end, you can perform array slicing in Python by considering default value for the third parameter. <\/p>\n\n\n\n<p>Let us take an example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport array\nimport numpy as np\n\n#array initialisation\narray_arr= array.array(&#039;i&#039;,&#x5B;1,2,3,4,5])\nnp_arr = np.array(&#x5B;6,7,8,9,10])\n\n#slicing array with 2 parameters\nprint(&quot;Sliced array: &quot;, array_arr&#x5B;2:5])\nprint(&quot;Sliced NumPy array: &quot;, np_arr&#x5B;1:4])\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nSliced array:  array(&#039;i&#039;, &#x5B;3, 4, 5])\nSliced NumPy array:  &#x5B;7 8 9]\n<\/pre><\/div>\n\n\n<p>In this case, too, the sliced <code>array<\/code> module array and <strong><code>NumPy<\/code><\/strong> array contain elements of indices specified <strong>start<\/strong> to <code>(stop-1)<\/code> with step set to <strong>1<\/strong>. The output is hence justified. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. With the step parameter<\/h2>\n\n\n\n<p>When all the three parameters are mentioned, you can perform array slicing in Python from index <strong>start<\/strong> to <strong>(stop-1)<\/strong> with each index jump equals to the given <strong>step<\/strong>. <\/p>\n\n\n\n<p>Look at the example below to have a clear understanding.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport array\nimport numpy as np\n\n#array initialisation\narray_arr= array.array(&#039;i&#039;,&#x5B;1,2,3,4,5,6,7,8,9,10])\nnp_arr = np.array(&#x5B;11,12,13,14,15,16,17,18,19,20])\n\n#slicing array with step parameter\nprint(&quot;Sliced array: &quot;, array_arr&#x5B;1:8:2])\nprint(&quot;Sliced NumPy array: &quot;, np_arr&#x5B;5:9:3])\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nSliced array:  array(&#039;i&#039;, &#x5B;2, 4, 6, 8])\nSliced NumPy array:  &#x5B;16 19]\n<\/pre><\/div>\n\n\n<p>Similarly, here we get sliced arrays with values from the arrays from given indices <strong>start<\/strong> to <strong>stop-1<\/strong>. The only difference here is the step value, this time it is specified as <strong>2<\/strong> and <strong>3<\/strong> for both <code>array<\/code> module array and <code>NumPy<\/code> array respectively. Hence this time each index jump is of the value of the given <strong>step<\/strong>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Array Slicing in Python with the slice() Method<\/h2>\n\n\n\n<p>The <code>slice()<\/code> method in Python returns a sequence of indices ranging from <strong>start<\/strong> to <strong>stop-1<\/strong> with the given <strong>step<\/strong> value. <\/p>\n\n\n\n<p>Similar to the previous cases, here also the default values of start and stop are 0 and the step is equal to 1.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport array\nimport numpy as np\n\n#array initialisation\narray_arr = array.array(&#039;i&#039;,&#x5B;1,2,3,4,5,6,7,8,9,10])\nnp_arr = np.array(&#x5B;11,12,13,14,15,16,17,18,19,20])\n\ns = slice(3,9,3)\n\n#slicing array with slice()\nprint(&quot;Sliced array: &quot;, array_arr&#x5B;s])\nprint(&quot;Sliced NumPy array: &quot;, np_arr&#x5B;s])\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nSliced array:  array(&#039;i&#039;, &#x5B;4, 7])\nSliced NumPy array:  &#x5B;14 17]\n<\/pre><\/div>\n\n\n<p>Here, firstly we have initialized two arrays, one from <code>array<\/code> module and the other from <code>NumPy<\/code> module. The <code>slice()<\/code> method is called with start, stop and step mentioned as <strong>3<\/strong>, <strong>9<\/strong> and <strong>3<\/strong> respectively. Hence, when we pass this sequence <code>s<\/code> to the arrays, we get sliced arrays with values containing the elements at indices <strong>3<\/strong> and <strong>6<\/strong>. <\/p>\n\n\n\n<p>Hence, the output is justified.<\/p>\n\n\n\n<p><strong>Note<\/strong>: Always the original array is kept intact and stays untouched. If needed, the sliced array can be stored in some variable. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>So in this tutorial, we got to learn about the concept of <strong>array slicing<\/strong> in Python. For any further questions feel free to comment it below. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a aria-label=\"Python Array Tutorials (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link rank-math-link\" href=\"https:\/\/www.askpython.com\/python\/array\/python-array-examples\" target=\"_blank\">Python Array Tutorials<\/a>,<\/li>\n\n\n\n<li><a aria-label=\"NumPy Arrays (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link rank-math-link\" href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-arrays\" target=\"_blank\">NumPy Arrays<\/a> in Python,<\/li>\n\n\n\n<li><a class=\"rank-math-link rank-math-link\" href=\"https:\/\/www.askpython.com\/python\/built-in-methods\/python-slice-function\">Python slice() Function<\/a>.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Array slicing in Python is a technique in programming that allows you to extract a portion of an array, or a sequence of elements in Python. This technique plays a crucial role in many data manipulation and analysis tasks, and is widely used in various applications ranging from scientific computing, web development, and data analysis. [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":4599,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-4595","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-array"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=4595"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4595\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/4599"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=4595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=4595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=4595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}