{"id":297,"date":"2020-10-06T10:22:01","date_gmt":"2020-10-06T10:22:01","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=297"},"modified":"2025-03-26T13:49:50","modified_gmt":"2025-03-26T13:49:50","slug":"python-list-slice","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-list-slice\/","title":{"rendered":"Python List Slice"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about Python list slice and how to use it to manipulate lists effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-python-list-slice-notation'>Introduction to Python List slice notation <a href=\"#introduction-to-python-list-slice-notation\" class=\"anchor\" id=\"introduction-to-python-list-slice-notation\" title=\"Anchor for Introduction to Python List slice notation\">#<\/a><\/h2>\n\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list\/\">Lists<\/a> support the slice notation that allows you to get a sublist from a list:<\/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\">sub_list = list&#91;begin: end: step]<\/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>In this syntax, the <code>begin<\/code>, <code>end<\/code>, and <code>step<\/code> arguments must be valid indexes. And they&#8217;re all optional.<\/p>\n\n\n\n<p>The <code>begin<\/code> index defaults to zero. The <code>end<\/code> index defaults to the length of the list. And the <code>step<\/code> index defaults to 1. <\/p>\n\n\n\n<p>The slice will start from the <code>begin<\/code> up to the <code>end<\/code> in the step of <code>step<\/code>.<\/p>\n\n\n\n<p>The <code>begin<\/code>, <code>end<\/code>, and <code>step<\/code> can be positive or negative. Positive values slice the list from the first element to the last element while negative values slice the list from the last element to the first element.<\/p>\n\n\n\n<p>In addition to extracting a sublist, you can use the list slice to change the list such as updating, resizing, and deleting a part of the list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='basic-python-list-slice-example'>Basic Python list slice example <a href=\"#basic-python-list-slice-example\" class=\"anchor\" id=\"basic-python-list-slice-example\" title=\"Anchor for Basic Python list slice example\">#<\/a><\/h2>\n\n\n\n<p>Suppose that you have the following list of strings:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" 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\">'orange'<\/span>, <span class=\"hljs-string\">'yellow'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'indigo'<\/span>, <span class=\"hljs-string\">'violet'<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><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>The following example uses the list slice to get a sublist from the <code>colors<\/code> list:<\/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\">colors = &#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'orange'<\/span>, <span class=\"hljs-string\">'yellow'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'indigo'<\/span>, <span class=\"hljs-string\">'violet'<\/span>]\nsub_colors = colors&#91;<span class=\"hljs-number\">1<\/span>:<span class=\"hljs-number\">4<\/span>]\n\nprint(sub_colors)\n<\/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=Y29sb3JzID0gWydyZWQnLCAnb3JhbmdlJywgJ3llbGxvdycsICdncmVlbicsICdibHVlJywgJ2luZGlnbycsICd2aW9sZXQnXQpzdWJfY29sb3JzID0gY29sb3JzWzE6NF0KCnByaW50KHN1Yl9jb2xvcnMp\" 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-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&#91;<span class=\"hljs-string\">'orange'<\/span>, <span class=\"hljs-string\">'yellow'<\/span>, <span class=\"hljs-string\">'green'<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><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>The <code>begin<\/code> index is 1, so the slice starts from the <code>'orange'<\/code> color. The <code>end<\/code> index is 4, therefore, the last element of the slice is <code>'green'<\/code>. <\/p>\n\n\n\n<p>As a result, the slice creates a new list with three colors: <code>['orange', 'yellow', 'green']<\/code>.<\/p>\n\n\n\n<p>This example doesn&#8217;t use the <code>step<\/code>, so the slice gets all values within the range without skipping any elements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='getting-the-n-first-elements-from-a-list'>Getting the n-first elements from a list <a href=\"#getting-the-n-first-elements-from-a-list\" class=\"anchor\" id=\"getting-the-n-first-elements-from-a-list\" title=\"Anchor for Getting the n-first elements from a list\">#<\/a><\/h2>\n\n\n\n<p>To get the n-first elements from a list, you omit the first argument:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">list&#91;:n]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><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>The following example returns a list that includes the first three elements from the colors list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" 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\">'orange'<\/span>, <span class=\"hljs-string\">'yellow'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'indigo'<\/span>, <span class=\"hljs-string\">'violet'<\/span>]\nsub_colors = colors&#91;:<span class=\"hljs-number\">3<\/span>]\n\nprint(sub_colors)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><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=Y29sb3JzID0gWydyZWQnLCAnb3JhbmdlJywgJ3llbGxvdycsICdncmVlbicsICdibHVlJywgJ2luZGlnbycsICd2aW9sZXQnXQpzdWJfY29sb3JzID0gY29sb3JzWzozXQoKcHJpbnQoc3ViX2NvbG9ycyk%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-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'orange'<\/span>, <span class=\"hljs-string\">'yellow'<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><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>Notice that the <code>colors[:3]<\/code> is equivalent to the <code>color[0:3]<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='retrieving-the-n-last-elements-from-a-list'>Retrieving the n-last elements from a list <a href=\"#retrieving-the-n-last-elements-from-a-list\" class=\"anchor\" id=\"retrieving-the-n-last-elements-from-a-list\" title=\"Anchor for Retrieving the n-last elements from a list\">#<\/a><\/h2>\n\n\n\n<p>To get the n-last elements of a list, you use the negative indexes. <\/p>\n\n\n\n<p>For example, the following returns a list that includes the last 3 elements of the <code>colors<\/code> list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" 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\">'orange'<\/span>, <span class=\"hljs-string\">'yellow'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'indigo'<\/span>, <span class=\"hljs-string\">'violet'<\/span>]\nsub_colors = colors&#91;<span class=\"hljs-number\">-3<\/span>:]\n\nprint(sub_colors)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><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=Y29sb3JzID0gWydyZWQnLCAnb3JhbmdlJywgJ3llbGxvdycsICdncmVlbicsICdibHVlJywgJ2luZGlnbycsICd2aW9sZXQnXQpzdWJfY29sb3JzID0gY29sb3JzWy0zOl0KCnByaW50KHN1Yl9jb2xvcnMp\" 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-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&#91;<span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'indigo'<\/span>, <span class=\"hljs-string\">'violet'<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><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<h2 class=\"wp-block-heading\" id='retrieving-every-nth-element-from-a-list'>Retrieving every nth element from a list <a href=\"#retrieving-every-nth-element-from-a-list\" class=\"anchor\" id=\"retrieving-every-nth-element-from-a-list\" title=\"Anchor for Retrieving every nth element from a list\">#<\/a><\/h2>\n\n\n\n<p>The following example uses the <code>step<\/code> to return a sublist that includes every 2nd element of the colors list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" 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\">'orange'<\/span>, <span class=\"hljs-string\">'yellow'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'indigo'<\/span>, <span class=\"hljs-string\">'violet'<\/span>]\nsub_colors = colors&#91;::<span class=\"hljs-number\">2<\/span>]\n\nprint(sub_colors)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><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=Y29sb3JzID0gWydyZWQnLCAnb3JhbmdlJywgJ3llbGxvdycsICdncmVlbicsICdibHVlJywgJ2luZGlnbycsICd2aW9sZXQnXQpzdWJfY29sb3JzID0gY29sb3JzWzo6Ml0KCnByaW50KHN1Yl9jb2xvcnMp\" 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=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'yellow'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'violet'<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><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<h2 class=\"wp-block-heading\" id='reversing-a-list'>Reversing a list <a href=\"#reversing-a-list\" class=\"anchor\" id=\"reversing-a-list\" title=\"Anchor for Reversing a list\">#<\/a><\/h2>\n\n\n\n<p>When you use a negative step, the slice includes the list of elements starting from the last element to the first element. In other words, it reverses the list. See the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" 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\">'orange'<\/span>, <span class=\"hljs-string\">'yellow'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'indigo'<\/span>, <span class=\"hljs-string\">'violet'<\/span>]\nreversed_colors = colors&#91;::<span class=\"hljs-number\">-1<\/span>]\n\nprint(reversed_colors)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><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=Y29sb3JzID0gWydyZWQnLCAnb3JhbmdlJywgJ3llbGxvdycsICdncmVlbicsICdibHVlJywgJ2luZGlnbycsICd2aW9sZXQnXQpyZXZlcnNlZF9jb2xvcnMgPSBjb2xvcnNbOjotMV0KCnByaW50KHJldmVyc2VkX2NvbG9ycyk%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=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&#91;<span class=\"hljs-string\">'violet'<\/span>, <span class=\"hljs-string\">'indigo'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'yellow'<\/span>, <span class=\"hljs-string\">'orange'<\/span>, <span class=\"hljs-string\">'red'<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><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<h2 class=\"wp-block-heading\" id='substituting-a-part-of-a-list'>Substituting a part of a list <a href=\"#substituting-a-part-of-a-list\" class=\"anchor\" id=\"substituting-a-part-of-a-list\" title=\"Anchor for Substituting a part of a list\">#<\/a><\/h2>\n\n\n\n<p>Besides extracting a part of a list, the list slice allows you to change the list element.<\/p>\n\n\n\n<p>The following example changes the first two elements in the colors list to the new values:<\/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\">'orange'<\/span>, <span class=\"hljs-string\">'yellow'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'indigo'<\/span>, <span class=\"hljs-string\">'violet'<\/span>]\ncolors&#91;<span class=\"hljs-number\">0<\/span>:<span class=\"hljs-number\">2<\/span>] = &#91;<span class=\"hljs-string\">'black'<\/span>, <span class=\"hljs-string\">'white'<\/span>]\n\nprint(colors)<\/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=Y29sb3JzID0gWydyZWQnLCAnb3JhbmdlJywgJ3llbGxvdycsICdncmVlbicsICdibHVlJywgJ2luZGlnbycsICd2aW9sZXQnXQpjb2xvcnNbMDoyXSA9IFsnYmxhY2snLCAnd2hpdGUnXQoKcHJpbnQoY29sb3JzKQ%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-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&#91;<span class=\"hljs-string\">'black'<\/span>, <span class=\"hljs-string\">'white'<\/span>, <span class=\"hljs-string\">'yellow'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'indigo'<\/span>, <span class=\"hljs-string\">'violet'<\/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<h2 class=\"wp-block-heading\" id='partially-replacing-and-resizing-a-list'>Partially replacing and resizing a list <a href=\"#partially-replacing-and-resizing-a-list\" class=\"anchor\" id=\"partially-replacing-and-resizing-a-list\" title=\"Anchor for Partially replacing and resizing a list\">#<\/a><\/h2>\n\n\n\n<p>The following example uses the list slice to replace the first and second elements with the new ones and also add a new element to the list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" 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\">'orange'<\/span>, <span class=\"hljs-string\">'yellow'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'indigo'<\/span>, <span class=\"hljs-string\">'violet'<\/span>]\nprint(<span class=\"hljs-string\">f\"The list has <span class=\"hljs-subst\">{len(colors)}<\/span> elements\"<\/span>)\n\ncolors&#91;<span class=\"hljs-number\">0<\/span>:<span class=\"hljs-number\">2<\/span>] = &#91;<span class=\"hljs-string\">'black'<\/span>, <span class=\"hljs-string\">'white'<\/span>, <span class=\"hljs-string\">'gray'<\/span>]\nprint(colors)\nprint(<span class=\"hljs-string\">f\"The list now has <span class=\"hljs-subst\">{len(colors)}<\/span> elements\"<\/span>)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><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=Y29sb3JzID0gWydyZWQnLCAnb3JhbmdlJywgJ3llbGxvdycsICdncmVlbicsICdibHVlJywgJ2luZGlnbycsICd2aW9sZXQnXQpwcmludChmIlRoZSBsaXN0IGhhcyB7bGVuKGNvbG9ycyl9IGVsZW1lbnRzIikKCmNvbG9yc1swOjJdID0gWydibGFjaycsICd3aGl0ZScsICdncmF5J10KcHJpbnQoY29sb3JzKQpwcmludChmIlRoZSBsaXN0IG5vdyBoYXMge2xlbihjb2xvcnMpfSBlbGVtZW50cyIp\" 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-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">The list has <span class=\"hljs-number\">7<\/span> elements\n&#91;<span class=\"hljs-string\">'black'<\/span>, <span class=\"hljs-string\">'white'<\/span>, <span class=\"hljs-string\">'gray'<\/span>, <span class=\"hljs-string\">'yellow'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'indigo'<\/span>, <span class=\"hljs-string\">'violet'<\/span>]\nThe list now has <span class=\"hljs-number\">8<\/span> elements<\/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<h2 class=\"wp-block-heading\" id='deleting-elements'>Deleting elements <a href=\"#deleting-elements\" class=\"anchor\" id=\"deleting-elements\" title=\"Anchor for Deleting elements\">#<\/a><\/h2>\n\n\n\n<p>The following shows how to use the list slice to delete the 3rd, 4th, and 5th elements from the <code>colors<\/code> list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" 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\">'orange'<\/span>, <span class=\"hljs-string\">'yellow'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>, <span class=\"hljs-string\">'indigo'<\/span>, <span class=\"hljs-string\">'violet'<\/span>]\n<span class=\"hljs-keyword\">del<\/span> colors&#91;<span class=\"hljs-number\">2<\/span>:<span class=\"hljs-number\">5<\/span>]\n\nprint(colors)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><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=Y29sb3JzID0gWydyZWQnLCAnb3JhbmdlJywgJ3llbGxvdycsICdncmVlbicsICdibHVlJywgJ2luZGlnbycsICd2aW9sZXQnXQpkZWwgY29sb3JzWzI6NV0KCnByaW50KGNvbG9ycyk%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-19\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&#91;<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'orange'<\/span>, <span class=\"hljs-string\">'indigo'<\/span>, <span class=\"hljs-string\">'violet'<\/span>]<\/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<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>Use a list slice to extract a sub-list from a list and modify the list.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=list-slice\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\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=\"297\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list-slice\/\"\n\t\t\t\tdata-post-title=\"Python List Slice\"\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=\"297\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list-slice\/\"\n\t\t\t\tdata-post-title=\"Python List Slice\"\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 various techniques to manipulate lists effectively using the Python list slice.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":29,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-297","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/297","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=297"}],"version-history":[{"count":5,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/297\/revisions"}],"predecessor-version":[{"id":7019,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/297\/revisions\/7019"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}