{"id":72,"date":"2024-01-25T09:37:27","date_gmt":"2024-01-25T09:37:27","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=72"},"modified":"2024-01-25T09:37:28","modified_gmt":"2024-01-25T09:37:28","slug":"python-list-slice","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-list-slice\/","title":{"rendered":"Python List Slice"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about Python list slice and how to use it to manipulate lists effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Python List slice notation<\/h2>\n\n\n\n<p>Lists\u00a0support the slice notation that allows you to get a sublist from a list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>sub_list = list&#91;begin: end: step]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this syntax, the&nbsp;<code>begin<\/code>,&nbsp;<code>end<\/code>, and&nbsp;<code>step<\/code>&nbsp;arguments must be valid indexes. And they\u2019re all optional.<\/p>\n\n\n\n<p>The&nbsp;<code>begin<\/code>&nbsp;index defaults to zero. The&nbsp;<code>end<\/code>&nbsp;index defaults to the length of the list. And the&nbsp;<code>step<\/code>&nbsp;index defaults to 1.<\/p>\n\n\n\n<p>The slice will start from the&nbsp;<code>begin<\/code>&nbsp;up to the&nbsp;<code>end<\/code>&nbsp;in the step of&nbsp;<code>step<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<code>begin<\/code>,&nbsp;<code>end<\/code>, and&nbsp;<code>step<\/code>&nbsp;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\">Python List slice examples<\/h2>\n\n\n\n<p>Let\u2019s take some examples of using the list slice.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Basic Python list slice example<\/h3>\n\n\n\n<p>Suppose that you have the following list of strings:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>colors = &#91;'red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following example uses the list slice to get a sublist from the\u00a0<code>colors<\/code>\u00a0list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>colors = &#91;'red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'] sub_colors = colors&#91;1:4] print(sub_colors) <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;'orange', 'yellow', 'green']<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>begin<\/code>&nbsp;index is 1, so the slice starts from the&nbsp;<code>'orange'<\/code>&nbsp;color. The&nbsp;<code>end<\/code>&nbsp;index is 4, therefore, the last element of the slice is&nbsp;<code>'green'<\/code>.<\/p>\n\n\n\n<p>As a result, the slice creates a new list with three colors:\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;'orange', 'yellow', 'green']<\/code>.<\/code><\/pre>\n\n\n\n<p>This example doesn\u2019t use the&nbsp;<code>step<\/code>, so the slice gets all values within the range without skipping any elements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2) Using Python List slice to get the n-first elements from a list<\/h3>\n\n\n\n<p>To get the n-first elements from a list, you omit the first argument:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>list&#91;:n]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following example returns a list that includes the first three elements from the colors list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>colors = &#91;'red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'] sub_colors = colors&#91;:3] print(sub_colors) <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;'red', 'orange', 'yellow']<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Notice that the&nbsp;<code>colors[:3]<\/code>&nbsp;is equivalent to the&nbsp;<code>color[0:3]<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3) Using Python List slice to get the n-last elements from a list<\/h3>\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\u00a0<code>colors<\/code>\u00a0list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>colors = &#91;'red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'] sub_colors = colors&#91;-3:] print(sub_colors) <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;'blue', 'indigo', 'violet']<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4) Using Python List slice to get every nth element from a list<\/h3>\n\n\n\n<p>The following example uses the\u00a0<code>step<\/code>\u00a0to return a sublist that includes every 2nd element of the colors list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>colors = &#91;'red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'] sub_colors = colors&#91;::2] print(sub_colors) <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;'red', 'yellow', 'blue', 'violet']<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5) Using Python List slice to reverse a list<\/h3>\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\n<pre class=\"wp-block-code\"><code><code>colors = &#91;'red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'] reversed_colors = colors&#91;::-1] print(reversed_colors) <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;'violet', 'indigo', 'blue', 'green', 'yellow', 'orange', 'red']<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6) Using Python List slice to substitute part of a list<\/h3>\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\n<pre class=\"wp-block-code\"><code><code>colors = &#91;'red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'] colors&#91;0:2] = &#91;'black', 'white'] print(colors) <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;'black', 'white', 'yellow', 'green', 'blue', 'indigo', 'violet']<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7) Using Python List slice to partially replace and resize a list<\/h3>\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\n<pre class=\"wp-block-code\"><code><code>colors = &#91;'red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'] print(f\"The list has {len(colors)} elements\") colors&#91;0:2] = &#91;'black', 'white', 'gray'] print(colors) print(f\"The list now has {len(colors)} elements\") <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>The list has 7 elements &#91;'black', 'white', 'gray', 'yellow', 'green', 'blue', 'indigo', 'violet'] The list now has 8 elements<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">8) Using Python list slice to delete elements<\/h3>\n\n\n\n<p>The following shows how to use the list slice to delete the 3rd, 4th, and 5th elements from the\u00a0<code>colors<\/code>\u00a0list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>colors = &#91;'red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'] del colors&#91;2:5] print(colors) <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;'red', 'orange', 'indigo', 'violet']<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about Python list slice and how to use it to manipulate lists effectively. Introduction to Python List slice notation Lists\u00a0support the slice notation that allows you to get a sublist from a list: In this syntax, the&nbsp;begin,&nbsp;end, and&nbsp;step&nbsp;arguments must be valid indexes. And they\u2019re all optional. The&nbsp;begin&nbsp;index defaults to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-72","post","type-post","status-publish","format-standard","hentry","category-5-lists"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/72","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=72"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/72\/revisions"}],"predecessor-version":[{"id":73,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/72\/revisions\/73"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}