{"id":1154,"date":"2020-11-15T09:38:47","date_gmt":"2020-11-15T09:38:47","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1154"},"modified":"2025-03-27T14:55:01","modified_gmt":"2025-03-27T14:55:01","slug":"python-slicing","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/advanced-python\/python-slicing\/","title":{"rendered":"Python Slicing in Depth"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about Python slicing and how to use it to extract data from and assign data to a sequence.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-slicing-review'>Python slicing review <a href=\"#python-slicing-review\" class=\"anchor\" id=\"python-slicing-review\" title=\"Anchor for Python slicing review\">#<\/a><\/h2>\n\n\n\n<p>So far you&#8217;ve learned about slicing such as <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list-slice\/\">list slicing<\/a>.<\/p>\n\n\n\n<p>Technically, slicing relies on indexing. Therefore, slicing only works with <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-sequences\/\">sequence types<\/a>.<\/p>\n\n\n\n<p>For mutable sequence types such as <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list\/\">lists<\/a>, you can use slicing to extract and assign data. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">colors = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'orange'<\/span>]\n\n<span class=\"hljs-comment\"># extract data<\/span>\nprint(colors&#91;<span class=\"hljs-number\">1<\/span>:<span class=\"hljs-number\">3<\/span>])\n\n<span class=\"hljs-comment\"># assign data<\/span>\ncolors&#91;<span class=\"hljs-number\">1<\/span>:<span class=\"hljs-number\">3<\/span>] = &#91;<span class=\"hljs-string\">'pink'<\/span>, <span class=\"hljs-string\">'black'<\/span>]\nprint(colors)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y29sb3JzID0gWydyZWQnLCAnZ3JlZW4nLCAnYmx1ZScsICdvcmFuZ2UnXQoKIyBleHRyYWN0IGRhdGEKcHJpbnQoY29sb3JzWzE6M10pCgojIGFzc2lnbiBkYXRhCmNvbG9yc1sxOjNdID0gWydwaW5rJywgJ2JsYWNrJ10KcHJpbnQoY29sb3JzKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">&#91;'green', 'blue']\n&#91;'red', 'pink', 'black', 'orange']<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, you can use slicing to extract data from immutable sequences. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">topic = <span class=\"hljs-string\">'Python Slicing'<\/span>\n\n<span class=\"hljs-comment\"># Extract data<\/span>\nprint(topic&#91;<span class=\"hljs-number\">0<\/span>:<span class=\"hljs-number\">6<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=dG9waWMgPSAnUHl0aG9uIFNsaWNpbmcnCgojIEV4dHJhY3QgZGF0YQpwcmludCh0b3BpY1swOjZdKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">Python<\/code><\/span><\/pre>\n\n\n<p>If you attempt to use slicing to assign data to an immutable sequence, you&#8217;ll get an error. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">topic&#91;<span class=\"hljs-number\">0<\/span>:<span class=\"hljs-number\">6<\/span>] = <span class=\"hljs-string\">'Java'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-built_in\">TypeError<\/span>: <span class=\"hljs-string\">'str'<\/span> object does not support item assignment<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The slicing <code>seq[start:stop]<\/code> returns the elements starting at the index <code>start<\/code> up to the index <code>stop - 1<\/code>. Therefore, it&#8217;s easier to visualize that the indexes are between the elements when you slice the sequence:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"641\" height=\"203\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/Python-Slicing.png\" alt=\"Python Slicing\" class=\"wp-image-1156\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/Python-Slicing.png 641w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/Python-Slicing-300x95.png 300w\" sizes=\"auto, (max-width: 641px) 100vw, 641px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id='python-slice-type'>Python slice type <a href=\"#python-slice-type\" class=\"anchor\" id=\"python-slice-type\" title=\"Anchor for Python slice type\">#<\/a><\/h2>\n\n\n\n<p>Everything in Python is an object including the slice. A slice is actually an object of the <code>slice<\/code> type. When you use the slicing notation:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">seq<\/span><span class=\"hljs-selector-attr\">&#91;start:stop]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>start:stop<\/code> is a <code>slice<\/code> object.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">slice(start, stop)<\/code><\/span><\/pre>\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">s = slice(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">3<\/span>)\n\n<span class=\"hljs-keyword\">print<\/span>(type(s))\n<span class=\"hljs-keyword\">print<\/span>(s.start, s.stop)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=cyA9IHNsaWNlKDEsIDMpCgpwcmludCh0eXBlKHMpKQpwcmludChzLnN0YXJ0LCBzLnN0b3Ap\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">class<\/span> '<span class=\"hljs-attr\">slice<\/span>'&gt;<\/span>\n1 3<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>So instead of using the slicing notation:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">colors<\/span><span class=\"hljs-selector-attr\">&#91;1:3]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>&#8230; you can use the slice object instead:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">colors = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'orange'<\/span>]\ns = slice(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">3<\/span>)\n\n<span class=\"hljs-keyword\">print<\/span>(colors&#91;s])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y29sb3JzID0gWydyZWQnLCAnZ3JlZW4nLCAnYmx1ZScsICdvcmFuZ2UnXQpzID0gc2xpY2UoMSwgMykKCnByaW50KGNvbG9yc1tzXSk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">&#91;'green', 'blue']<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='python-slicing-start-and-stop-bounds'>Python Slicing: start and stop bounds <a href=\"#python-slicing-start-and-stop-bounds\" class=\"anchor\" id=\"python-slicing-start-and-stop-bounds\" title=\"Anchor for Python Slicing: start and stop bounds\">#<\/a><\/h2>\n\n\n\n<p>The slice <code>seq[start:stop]<\/code> selects elements starting at the index <code>start<\/code> and stopping at the index <code>stop<\/code> (excluding the element at the index <code>stop<\/code>).<\/p>\n\n\n\n<p>In other words, it returns all elements of the sequence at the index <code>n<\/code> where <code>n<\/code> satisfies the following expression:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">start &lt;= n &lt; stop<\/code><\/span><\/pre>\n\n\n<p>When <code>start<\/code> or <code>stop<\/code> is greater than the length of the sequence:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">len(seq)<\/code><\/span><\/pre>\n\n\n<p>&#8230; Python uses <code>len(seq)<\/code> for the <code>start<\/code> or <code>stop<\/code>.<\/p>\n\n\n\n<p>Both <code>start<\/code> and <code>stop<\/code> are optional. The <code>start<\/code> defaults to 0 and <code>stop<\/code> defaults to <code>len(seq)<\/code> when you don&#8217;t specify it.<\/p>\n\n\n\n<p>The following example returns the entire list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">colors = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'orange'<\/span>]\n<span class=\"hljs-keyword\">print<\/span>(colors&#91;<span class=\"hljs-number\">0<\/span>:<span class=\"hljs-number\">100<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y29sb3JzID0gWydyZWQnLCAnZ3JlZW4nLCAnYmx1ZScsICdvcmFuZ2UnXQpwcmludChjb2xvcnNbMDoxMDBdKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">&#91;'red', 'green', 'blue', 'orange']<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Since the stop bound is 100, Python uses the <code>len(colors)<\/code> for the <code>stop<\/code> bound.<\/p>\n\n\n\n<p>The following example returns an empty list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">colors = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'orange'<\/span>]\nprint(colors&#91;<span class=\"hljs-number\">10<\/span>:])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y29sb3JzID0gWydyZWQnLCAnZ3JlZW4nLCAnYmx1ZScsICdvcmFuZ2UnXQpwcmludChjb2xvcnNbMTA6XSk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Since the <code>start<\/code> is 10, Python assigns the <code>len(colors)<\/code> to it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='negative-start-and-stop-bounds'>Negative start and stop bounds <a href=\"#negative-start-and-stop-bounds\" class=\"anchor\" id=\"negative-start-and-stop-bounds\" title=\"Anchor for Negative start and stop bounds\">#<\/a><\/h3>\n\n\n\n<p>The slice object also accepts negative <code>start<\/code> and <code>stop<\/code> bounds. The following example uses the negative start and stop bounds to slice a list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">colors = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'orange'<\/span>]\n\nprint(colors&#91;<span class=\"hljs-number\">-4<\/span>:<span class=\"hljs-number\">-2<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y29sb3JzID0gWydyZWQnLCAnZ3JlZW4nLCAnYmx1ZScsICdvcmFuZ2UnXQoKcHJpbnQoY29sb3JzWy00Oi0yXSk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">&#91;'red', 'green']<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To get the <code>'blue'<\/code> and <code>'orange'<\/code> elements from the <code>colors<\/code> list, you can combine the negative and positive bounds:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">colors = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'orange'<\/span>]\nprint(colors&#91;<span class=\"hljs-number\">-2<\/span>:<span class=\"hljs-number\">4<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y29sb3JzID0gWydyZWQnLCAnZ3JlZW4nLCAnYmx1ZScsICdvcmFuZ2UnXQpwcmludChjb2xvcnNbLTI6NF0p\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">&#91;'blue', 'orange']<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"635\" height=\"164\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/Python-Slicing-Negative-Bounds.png\" alt=\"\" class=\"wp-image-1157\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/Python-Slicing-Negative-Bounds.png 635w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/Python-Slicing-Negative-Bounds-300x77.png 300w\" sizes=\"auto, (max-width: 635px) 100vw, 635px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='the-step-value'>The step value <a href=\"#the-step-value\" class=\"anchor\" id=\"the-step-value\" title=\"Anchor for The step value\">#<\/a><\/h2>\n\n\n\n<p>Slices support the third argument, which is the step value. The step value defaults to 1 if you don&#8217;t specify it:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">seq&#91;star:stop:step]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It&#8217;s equivalent to:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">s = slice(start, stop, step) \nseq&#91;s]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p> See the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">colors = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'orange'<\/span>]\nprint(colors&#91;<span class=\"hljs-number\">0<\/span>:<span class=\"hljs-number\">4<\/span>:<span class=\"hljs-number\">2<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y29sb3JzID0gWydyZWQnLCAnZ3JlZW4nLCAnYmx1ZScsICdvcmFuZ2UnXQpwcmludChjb2xvcnNbMDo0OjJdKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">&#91;'red', 'blue']<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='the-indices-method'>The indices method <a href=\"#the-indices-method\" class=\"anchor\" id=\"the-indices-method\" title=\"Anchor for The indices method\">#<\/a><\/h2>\n\n\n\n<p>A slice object essentially defines a sequence of indices for selecting elements of a sequence.<\/p>\n\n\n\n<p>To make it more convenient, the <code>slice<\/code> type has the <code>indices<\/code> method that returns the equivalent range <code>(start, stop, step)<\/code> for any slice of a sequence with a specified length:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">slice<\/span>(<span class=\"hljs-selector-tag\">start<\/span>, <span class=\"hljs-selector-tag\">stop<\/span>, <span class=\"hljs-selector-tag\">step<\/span>)<span class=\"hljs-selector-class\">.indices<\/span>(<span class=\"hljs-selector-tag\">length<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It returns a new tuple:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">(i, j, k)<\/code><\/span><\/pre>\n\n\n<p>And you can use the values of this tuple to generate a list of indices using the <code>range<\/code> function. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">colors = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'orange'<\/span>]\n\ns = slice(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">2<\/span>)\nt = s.indices(len(colors))\n\n<span class=\"hljs-keyword\">for<\/span> index <span class=\"hljs-keyword\">in<\/span> range(*t):\n    print(colors&#91;index])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y29sb3JzID0gWydyZWQnLCAnZ3JlZW4nLCAnYmx1ZScsICdvcmFuZ2UnXQoKcyA9IHNsaWNlKDAsIDQsIDIpCnQgPSBzLmluZGljZXMobGVuKGNvbG9ycykpCgpmb3IgaW5kZXggaW4gcmFuZ2UoKnQpOgogICAgcHJpbnQoY29sb3JzW2luZGV4XSk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">red\nblue<\/code><\/span><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, create a slice object whose start is 0, stop is 4, and step is 2.<\/li>\n\n\n\n<li>Second, return a tuple of indices of the slice of the sequence whose length is the length of the colors list.<\/li>\n\n\n\n<li>Third, pass the result tuple to the range function to select elements from the colors list.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Slicing only works for sequence types including mutable and immutable sequences.<\/li>\n\n\n\n<li>A slice is an object the <code>slice<\/code> type.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1154\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-slicing\/\"\n\t\t\t\tdata-post-title=\"Python Slicing in Depth\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1154\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-slicing\/\"\n\t\t\t\tdata-post-title=\"Python Slicing in Depth\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about Python slicing and how to use it to extract data from and assign data to a sequence.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":757,"menu_order":16,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1154","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=1154"}],"version-history":[{"count":2,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1154\/revisions"}],"predecessor-version":[{"id":7128,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1154\/revisions\/7128"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/757"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=1154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}