{"id":25772,"date":"2020-12-29T18:09:52","date_gmt":"2020-12-29T12:39:52","guid":{"rendered":"https:\/\/www.guru99.com\/?p=25772"},"modified":"2026-06-23T19:19:09","modified_gmt":"2026-06-23T13:49:09","slug":"1-in-python","status":"publish","type":"post","link":"https:\/\/www.guru99.com\/1-in-python.html","title":{"rendered":"[::-1] in Python with Examples","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"<div class=\"readmorecontainer summary\">\n<p><strong>\u26a1 Smart Summary<\/strong><\/p>\n\n<p>[::-1] in Python is a powerful slice expression that reverses any sequence type. Lessons cover indexing fundamentals, slice parameters, the difference between a[-1] and a[::-1], list reversal, and worked examples.<\/p>\n\n<div class=\"expandable-content\" id=\"expandableContent\">\n<ul style=\"list-style-type: none;\">\n<li>\ud83d\udd01 <strong>Reverse Slice:<\/strong> [::-1] uses empty start, empty stop, step -1.<\/li>\n<li>\ud83d\udccd <strong>Indexing:<\/strong> Positive indices start at zero; negative count from the end.<\/li>\n<li>\ud83c\udd9a <strong>[-1] vs [::-1]:<\/strong> [-1] returns the last element; [::-1] returns the whole reversed sequence.<\/li>\n<li>\ud83d\udccb <strong>Lists and Strings:<\/strong> Works on any indexed iterable including lists, strings, tuples, bytes.<\/li>\n<li>\u26a1 <strong>Fast and Idiomatic:<\/strong> Implemented in C, far faster than Python loops.<\/li>\n<li>\ud83e\udd16 <strong>AI Snippet Aid:<\/strong> AI tools explain slice parameters and convert loops to slices.<\/li>\n<\/ul>\n<\/div>\n\n    <div class=\"btn-container\">            \n        <a href=\"javascript:void(0);\" id=\"readMoreBtn\" data-lasso-id=\"99101246\">\n            <span id=\"buttonText\">Read More<\/span>\n            <svg class=\"chevron\" id=\"chevron\" fill=\"none\" stroke=\"currentColor\" viewbox=\"0 0 24 24\">\n                <path stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M19 9l-7 7-7-7\"><\/path>\n            <\/svg>\n        <\/a>            \n    <\/div>\n<\/div>\n\n\n<p style=\"text-align: center;padding-top: 15px;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.guru99.com\/images\/python-slice-reverse-with-examples.png\" alt=\"Python Slice Reverse\" width=\"700\" height=\"250\" class=\"\"><\/p>\n\n<style>.kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-table-of-content-wrap{padding-top:var(--global-kb-spacing-sm, 1.5rem);padding-right:var(--global-kb-spacing-sm, 1.5rem);padding-bottom:var(--global-kb-spacing-sm, 1.5rem);padding-left:var(--global-kb-spacing-sm, 1.5rem);background-color:#edf2f7;border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);max-width:450px;}.kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-table-of-contents-title-wrap{padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-table-of-contents-title{font-weight:regular;font-style:normal;}.kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-table-of-content-wrap .kb-table-of-content-list{font-weight:regular;font-style:normal;margin-top:var(--global-kb-spacing-sm, 1.5rem);margin-right:0px;margin-bottom:0px;margin-left:0px;}.kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-toggle-icon-style-basiccircle .kb-table-of-contents-icon-trigger:after, .kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-toggle-icon-style-basiccircle .kb-table-of-contents-icon-trigger:before, .kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-toggle-icon-style-arrowcircle .kb-table-of-contents-icon-trigger:after, .kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-toggle-icon-style-arrowcircle .kb-table-of-contents-icon-trigger:before, .kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-toggle-icon-style-xclosecircle .kb-table-of-contents-icon-trigger:after, .kb-table-of-content-nav.kb-table-of-content-id_ba85a2-6e .kb-toggle-icon-style-xclosecircle .kb-table-of-contents-icon-trigger:before{background-color:#edf2f7;}<\/style>\n\n<h2>What is [::-1] in Python?<\/h2>\n<p>Python has many different syntax and applications when it comes to utilizing 1. It could either be represented as <code>[-1]<\/code>, or it can also be described as <code>[::-1]<\/code>.<\/p>\n<p>The square brackets in the above notation generally specify more than one item in the list. When using square brackets, one specifies either multiple items or a single item in the list.<\/p>\n<p>Whenever a programmer defines [::-1], it suggests that the program has to traverse from start to end in a given list. You can do indexing in python, which helps to slice and dice an iterable sequence such as a list or string.<\/p>\n<h2>What is the type of [::-1] in Python?<\/h2>\n<p>There are two ways to represent 1 in python as described below: &#8211;<\/p>\n<ul>\n<li>It can be represented in a simple square bracket with no colon and another method with double colon syntax.<\/li>\n<li>An iterable sequence such as a list or string can be parsed to item level using [] brackets.<\/li>\n<li>Within the [] brackets, one has to provide different indexes.<\/li>\n<\/ul>\n<div class='code-block code-block-2' style='margin: 8px 0; clear: both;'>\n<style>\n.guru99_incontent_21 {\n\tmin-height: 280px !important;\n  display: flex;\n  align-items: center;\n  justify-content: center;\n}\n<\/style>\n\n<div align=\"center\" id=\"guru99_mobile_display\" class=\"guru99_incontent_21\">\n    \n  <script>\n    googletag.cmd.push(function() { googletag.display('guru99_mobile_display'); });\n  <\/script>\n<\/div><\/div>\n\n<p>Following is the syntax for the List as shown below: &#8211;<\/p>\n<p><strong>Syntax: &#8211;<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>SEQUENCE [START: <span class=\"lkw\">STOP<\/span>: STEP]\n<\/pre>\n<\/div>\n<p>The above syntax is a general representation. The syntax for square brackets with no colon is represented as follows:<\/p>\n<p><strong>Syntax: &#8211;<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>SEQUENCE [-1]<\/pre>\n<\/div>\n<p>The syntax for square brackets with two colon is represented as follows:<\/p>\n<p><strong>Syntax: &#8211;<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>SEQUENCE[::-1]\n<\/pre>\n<\/div>\n<p><strong>Explanation: &#8211;<\/strong><\/p>\n<ul>\n<li>The start parameter represents the start index number.<\/li>\n<li>The stop parameter represents the end or last index number.<\/li>\n<li>The parameter step is an optional parameter that provides an increment for a selection. In the syntax, the step count is initialized as -1.<\/li>\n<li>A sequence could be a string, list, or dictionary in the above syntax.<\/li>\n<\/ul>\n<h2>Difference between a[-1] and a[::-1] in Python<\/h2>\n<p>A [-1] is used for negative indexes and helps select items in reverse order in a given list. It signifies the beginning of the list from the end of the reverse order. Here, [-1] signifies the first item from the last position. The following is an example that explains the usage of A[-1]<\/p>\n<p><strong>Example: &#8211;<\/strong><\/p>\n<p><strong>Python Code:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>b= [7,4,5,6]\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\"The list <span class=\"lkw\">is<\/span>\"<\/span>, b)\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\" The last element <span class=\"lkw\">in<\/span> the list <span class=\"lkw\">is<\/span>\"<\/span>, b [-1])\n<\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>The list <span class=\"lkw\">is<\/span> [7, 4, 5, 6]\nThe last element <span class=\"lkw\">in<\/span> the list <span class=\"lkw\">is<\/span> 6\n<\/pre>\n<\/div>\n<p>A[::-1] and A[-1] look similar in terms of execution but are different in terms of syntax. The first syntax has three parameters, whereas the latter syntax has only a single parameter.<\/p>\n<p>The first two parameters present in A[::-1] represent the start and stop for a list. If the start and stop parameters are omitted, it returns all the items in the list.<\/p>\n<p>This last parameter shows the output in reverse order. This parameter is optional.<\/p>\n<p>The following is an example that explains the usage of A[::-1]<\/p>\n<p><strong>Example: &#8211;<\/strong><\/p>\n<p><strong>Python Code:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>b= [7,4,5,6]\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\"The list <span class=\"lkw\">is<\/span>\"<\/span>, b)\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\" The reversed list <span class=\"lkw\">as<\/span> determined <span class=\"lkw\">is<\/span>\"<\/span>, b [::-1])\n<\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>The list <span class=\"lkw\">is<\/span> [7, 4, 5, 6]\nThe reversed list <span class=\"lkw\">as<\/span> determined <span class=\"lkw\">is<\/span> [6, 5, 4, 7]\n<\/pre>\n<\/div>\n<p><strong>Explanation:<\/strong><\/p>\n<p>The above Python code does not produce the same results for the list created. Instead, it provides a reverse list for the base list provided as the input value.<\/p>\n<div class='code-block code-block-3' style='margin: 8px 0; clear: both;'>\n<style>\n.guru99_incontent_21 {\n\tmin-height:280px !important;\n}\n<\/style>\n\n<!-- Tag ID: guru99_static_3 -->\n<div id='guru99_incontent_2' class=\"guru99_incontent_21\">\n  <script>\n    googletag.cmd.push(function() { googletag.display('guru99_incontent_2'); });\n  <\/script>\n<\/div>\n<div class='yarpp yarpp-related yarpp-related-shortcode yarpp-template-yarpp-template-custom'>\n<!-- YARPP List -->\n<strong class=\"you-might-like\">Don't Miss:<\/strong><ul>\n<li><a href=\"https:\/\/www.guru99.com\/execute-python-online.html\" rel=\"bookmark\" title=\"Online Python Compiler (Editor \/ Interpreter \/ IDE) to Run Code\">Online Python Compiler (Editor \/ Interpreter \/ IDE) to Run Code<\/a><\/li>\n<li><a href=\"https:\/\/www.guru99.com\/python-class-objects-object-oriented-programming-oop-s.html\" rel=\"bookmark\" title=\"Python OOPs: Class, Object, Inheritance and Constructor\">Python OOPs: Class, Object, Inheritance and Constructor<\/a><\/li>\n<li><a href=\"https:\/\/www.guru99.com\/python-vs-go.html\" rel=\"bookmark\" title=\"Go Vs. Python: What&#8217;s the Difference?\">Go Vs. Python: What&#8217;s the Difference?<\/a><\/li>\n<li><a href=\"https:\/\/www.guru99.com\/python-howto-remove-duplicates.html\" rel=\"bookmark\" title=\"How to Remove Duplicates from a List in Python\">How to Remove Duplicates from a List in Python<\/a><\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n<p>The difference above hinges on Python&#8217;s indexing rules.<\/p>\n<h2>How to perform Indexing in Python?<\/h2>\n<p>In order to access an item in an iterable python list, there is a need to perform indexing in the list or use an index as applied with the position of the item present in the list.<\/p>\n<p>Python performs zero-based indexing for lists. In the case of zero-based indexing, the first item in the list is assigned as position 0.<\/p>\n<p>Let\u2019s  take an example and try to access the beginning and second items in a python list.<\/p>\n<p><strong>Example: &#8211;<\/strong><\/p>\n<p><strong>Python Code:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>b= [7,4,5,6]\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\"The list <span class=\"lkw\">is<\/span>\"<\/span>, b)\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\" The reverse ordered list <span class=\"lkw\">as<\/span> determined <span class=\"lkw\">is<\/span>\"<\/span>, b [::-1])\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\" The second item found <span class=\"lkw\">in<\/span> list <span class=\"lkw\">is<\/span>\"<\/span>, b [1])\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\" The first <span class=\"lkw\">or<\/span> beginning item <span class=\"lkw\">in<\/span> the list <span class=\"lkw\">is<\/span>\"<\/span>, b [0])\n<\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>The list <span class=\"lkw\">is<\/span> [7, 4, 5, 6]\nThe reverse ordered list <span class=\"lkw\">as<\/span> determined <span class=\"lkw\">is<\/span> [6, 5, 4, 7]\nThe second found item <span class=\"lkw\">in<\/span> the list <span class=\"lkw\">is<\/span> 4\nThe first <span class=\"lkw\">or<\/span> beginning item <span class=\"lkw\">in<\/span> the list <span class=\"lkw\">is<\/span> 7\n<\/pre>\n<\/div>\n<p><strong>Explanation:<\/strong><\/p>\n<p>As seen in the above code, to access the first element in the list, 0 was applied as an index within the square of the list type. Similarly, 1 was applied as an index within the list type&#8217;s square to access the list&#8217;s second element. The indexing of the list can also apply to dictionaries, strings, and tuples.<\/p>\n<p>Let us take an example of the indexing of <a href=\"\/learning-python-strings-replace-join-split-reverse.html\" data-lasso-id=\"489906\">string type in python<\/a>.<\/p>\n<p><strong>Example: <\/strong><\/p>\n<p><strong>Python Code:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>b= <span class=\"lstr\">\"GURU99\"<\/span>\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\"The list <span class=\"lkw\">is<\/span>\"<\/span>, b)\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\" The reverse ordered list <span class=\"lkw\">as<\/span> determined <span class=\"lkw\">is<\/span>\"<\/span>, b [::-1])\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\" The second item found <span class=\"lkw\">in<\/span> list <span class=\"lkw\">is<\/span>\"<\/span>, b [1])\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\" The first <span class=\"lkw\">or<\/span> beginning item <span class=\"lkw\">in<\/span> the list <span class=\"lkw\">is<\/span>\"<\/span>, b [0])\n<\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>The list <span class=\"lkw\">is<\/span> GURU99\nThe reverse ordered list <span class=\"lkw\">as<\/span> determined <span class=\"lkw\">is<\/span> 99URUG\nThe second item found <span class=\"lkw\">in<\/span> the list <span class=\"lkw\">is<\/span> U\nThe first <span class=\"lkw\">or<\/span> beginning item <span class=\"lkw\">in<\/span> the list <span class=\"lkw\">is<\/span> G\n<\/pre>\n<\/div>\n<p><strong>Explanation:<\/strong><\/p>\n<p>As seen above, the output shares the first and second elements of string \u201cGURU99\u201d. A string has to be initialized using double quotes. Python also allows us to perform negative indexing of iterable lists, strings, or dictionaries.<\/p>\n<p>By providing a negative index, one can access the last as well as the second last element of the list. Let us take an example of the negative indexing of a string, as shown below.<\/p>\n<p><strong>Example: &#8211;<\/strong><\/p>\n<p><strong>Python Code:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>b= <span class=\"lstr\">\"GURU99\"<\/span>\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\"The list <span class=\"lkw\">is<\/span>\"<\/span>, b)\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\" The reverse ordered list <span class=\"lkw\">as<\/span> determined <span class=\"lkw\">is<\/span>\"<\/span>, b [::-1])\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\" The second item found <span class=\"lkw\">in<\/span> list <span class=\"lkw\">is<\/span>\"<\/span>, b [-2])\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\" The first <span class=\"lkw\">or<\/span> beginning item <span class=\"lkw\">in<\/span> the list <span class=\"lkw\">is<\/span>\"<\/span>, b [-1])\n<\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>The list <span class=\"lkw\">is<\/span> GURU99\nThe reverse ordered list <span class=\"lkw\">as<\/span> determined <span class=\"lkw\">is<\/span> 99URUG\nThe second item found <span class=\"lkw\">in<\/span> the list <span class=\"lkw\">is<\/span> 9\nThe first <span class=\"lkw\">or<\/span> beginning item <span class=\"lkw\">in<\/span> the list <span class=\"lkw\">is<\/span> 9\n<\/pre>\n<\/div>\n<div class='code-block code-block-4' style='margin: 8px 0; clear: both;'>\n<style>\n.guru99_incontent_31 {\n\tmin-height:280px !important;\n}\n<\/style>\n\n<!-- Tag ID: guru99_static_4 -->\n<div id='guru99_incontent_3' class=\"guru99_incontent_31\">\n  <script>\n    googletag.cmd.push(function() { googletag.display('guru99_incontent_3'); });\n  <\/script>\n<\/div><\/div>\n\n<p>Indexing returns one element; slicing returns a range.<\/p>\n<h2>Role of slicing in Python<\/h2>\n<p>In simpler words, a slice means to cut something. It allows dicing a list in python.<\/p>\n<p>Here, how slicing is important in Python:<\/p>\n<ul>\n<li>It allows access to specific elements of an iterable list or a string.<\/li>\n<li>It also helps in deleting and modifying the string or a list.<\/li>\n<li>It makes iterable sequence to be concise and readable.<\/li>\n<li>It enables us to access multiple items compared to a single element under the indexing concept.<\/li>\n<li>It utilizes indexes to procure specific item ranges in the iterable sequence.<\/li>\n<li>The indexes under python are always zero-based, whether doing slicing or indexing over an iterable sequence.<\/li>\n<\/ul>\n<p>Slicing can be achieved in two basic forms.<\/p>\n<ul>\n<li>The first form is to provide start and stop index parameters of a sequence. By doing so, the sequence would return all possible items between the start of the sequence and the stop [end -1] of the sequence.<\/li>\n<li>The second method is to provide the sequence&#8217;s start and stop index parameters along with the step. A step enables the return of specific or selected items within a range of items between the start and stop.<\/li>\n<\/ul>\n<p>Following is the syntax for the first form of slicing: &#8211;<\/p>\n<p><strong>Syntax: <\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>SEQUENCE [START: <span class=\"lkw\">STOP<\/span>]\n<\/pre>\n<\/div>\n<p>Following is the syntax for the second form of slicing: &#8211;<\/p>\n<p><strong>Syntax: <\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>SEQUENCE [START: <span class=\"lkw\">STOP<\/span>: STEP]\n<\/pre>\n<\/div>\n<p>Let us take the example of the first form of slicing example as shown below.<\/p>\n<p><strong>Example<\/strong><\/p>\n<p><strong>Python Code:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>b= <span class=\"lstr\">\"GURU99\"<\/span>\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\"The list <span class=\"lkw\">is<\/span>\"<\/span>, b)\nSubstring=b[0:3]\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\" The substring <span class=\"lkw\">in<\/span> the list <span class=\"lkw\">is<\/span>\"<\/span>, Substring)\n<\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>The list <span class=\"lkw\">is<\/span> GURU99\nThe substring <span class=\"lkw\">in<\/span> the list <span class=\"lkw\">is<\/span> GUR\n<\/pre>\n<\/div>\n<p>Let us take the example of the second form of slicing example as shown below.<\/p>\n<p><strong>Example<\/strong><\/p>\n<p><strong>Python Code:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>b= <span class=\"lstr\">\"GURU99\"<\/span>\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\"The list <span class=\"lkw\">is<\/span>\"<\/span>, b)\nSubstring=b[0:9:2]\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\" The substring <span class=\"lkw\">in<\/span> the list <span class=\"lkw\">is<\/span>\"<\/span>, Substring)\n<\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>The list <span class=\"lkw\">is<\/span> GURU99\nThe substring <span class=\"lkw\">in<\/span> the list <span class=\"lkw\">is<\/span> GR9\n<\/pre>\n<\/div>\n<p>Python also performs negative slicing for an iterable sequence. It enables you to access a range of items or elements from the sequence\u2019s end.<\/p>\n<p>Let us take an example to access the last three elements from the sequence as shown below.<\/p>\n<p><strong>Example<\/strong><\/p>\n<p><strong>Python Code:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>b= <span class=\"lstr\">\"GURU99\"<\/span>\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\"The list <span class=\"lkw\">is<\/span>\"<\/span>, b)\nSubstring=b[-5:-1]\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\" The substring <span class=\"lkw\">in<\/span> the list <span class=\"lkw\">is<\/span>\"<\/span>, Substring)\n<\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>The list <span class=\"lkw\">is<\/span> GURU99\nThe substring <span class=\"lkw\">in<\/span> the list <span class=\"lkw\">is<\/span> URU9\n<\/pre>\n<\/div>\n<h2>How to reverse a Python list using [::-1] in Python?<\/h2>\n<p>The negative slicing and negative indexing can be applied to reverse a string or list in python. Let us take a string named \u201cGURU99\u201d to illustrate an example.<\/p>\n<p><strong>Example<\/strong><\/p>\n<p><strong>Python Code:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>b= <span class=\"lstr\">\"GURU99\"<\/span>\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\"The list <span class=\"lkw\">is<\/span>\"<\/span>, b)\nSubstring=b[::-1]\n<span class=\"lkw\">print<\/span> (<span class=\"lstr\">\" The reverse of string GURU99 <span class=\"lkw\">is<\/span>\"<\/span>, Substring)\n<\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>The list <span class=\"lkw\">is<\/span> GURU99\nThe substring <span class=\"lkw\">in<\/span> the list <span class=\"lkw\">is<\/span> 99URUG\n<\/pre>\n<\/div>\n<p><strong>Explanation:<\/strong><\/p>\n<p>Here, the code fetches all the string elements from the last position. It starts with -1 and fetches all the items or elements from the last position. The above line of code tells python to step back from the last element and step up to the first element, which results in a reverse list or a string.<\/p>\n<h2>Example of [::-1] in Python<\/h2>\n<p>Slicing or indexing can be employed to extract a smaller list from a more extensive list. Similarly, it can be used to extract a substring from a larger string.<\/p>\n<p>Let us take an example of how to use 1 in python to extract a smaller list out of a bigger list:<\/p>\n<p><strong>Example<\/strong><\/p>\n<p><strong>Python Code:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>b = [1, 2, 3, 4, 5, 6, 7]\nslist = b[1::]\n<span class=\"lkw\">print<\/span>(<span class=\"lstr\">\"the smaller list <span class=\"lkw\">from<\/span> larger list <span class=\"lkw\">is<\/span>:\"<\/span>,slist)\n<\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"cobalt-light-code\">\n<pre>the smaller list <span class=\"lkw\">from<\/span> the larger list <span class=\"lkw\">is<\/span>: [2, 3, 4, 5, 6, 7]\n<\/pre>\n<\/div>\n<p><strong>Explanation:<\/strong><\/p>\n<p>In the above code, leaving the first index, python extracts all elements starting from position 2 of the list and creates a smaller list. This has happened as python follows zero-indexing when applying indexes to the listed elements&#8217; positions.<\/p>\n<h2 class=\"toc-ignore\">FAQs<\/h2>\n\n<style>.kt-accordion-id_2eaf18-ec .kt-accordion-inner-wrap{column-gap:var(--global-kb-gap-md, 2rem);row-gap:10px;}.kt-accordion-id_2eaf18-ec .kt-accordion-panel-inner{border-top-width:0px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;background:#ffffff;padding-top:var(--global-kb-spacing-sm, 1.5rem);padding-right:var(--global-kb-spacing-sm, 1.5rem);padding-bottom:var(--global-kb-spacing-sm, 1.5rem);padding-left:var(--global-kb-spacing-sm, 1.5rem);}.kt-accordion-id_2eaf18-ec > .kt-accordion-inner-wrap > .wp-block-kadence-pane > .kt-accordion-header-wrap > .kt-blocks-accordion-header{border-top-color:#eeeeee;border-right-color:#eeeeee;border-bottom-color:#eeeeee;border-left-color:#eeeeee;border-top-width:2px;border-right-width:2px;border-bottom-width:2px;border-left-width:2px;border-top-left-radius:0px;border-top-right-radius:0px;border-bottom-right-radius:0px;border-bottom-left-radius:0px;background:#ffffff;font-size:18px;line-height:24px;font-weight:bold;color:#444444;padding-top:14px;padding-right:16px;padding-bottom:14px;padding-left:16px;}.kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basiccircle ):not( .kt-accodion-icon-style-xclosecircle ):not( .kt-accodion-icon-style-arrowcircle )  > .kt-accordion-inner-wrap > .wp-block-kadence-pane > .kt-accordion-header-wrap .kt-blocks-accordion-icon-trigger:after, .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basiccircle ):not( .kt-accodion-icon-style-xclosecircle ):not( .kt-accodion-icon-style-arrowcircle )  > .kt-accordion-inner-wrap > .wp-block-kadence-pane > .kt-accordion-header-wrap .kt-blocks-accordion-icon-trigger:before{background:#444444;}.kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-blocks-accordion-icon-trigger{background:#444444;}.kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-blocks-accordion-icon-trigger:after, .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-blocks-accordion-icon-trigger:before{background:#ffffff;}.kt-accordion-id_2eaf18-ec > .kt-accordion-inner-wrap > .wp-block-kadence-pane > .kt-accordion-header-wrap > .kt-blocks-accordion-header:hover, \n\t\t\t\tbody:not(.hide-focus-outline) .kt-accordion-id_2eaf18-ec .kt-blocks-accordion-header:focus-visible{color:#444444;background:#ffffff;border-top-color:#d4d4d4;border-right-color:#d4d4d4;border-bottom-color:#d4d4d4;border-left-color:#d4d4d4;}.kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basiccircle ):not( .kt-accodion-icon-style-xclosecircle ):not( .kt-accodion-icon-style-arrowcircle ) .kt-accordion-header-wrap .kt-blocks-accordion-header:hover .kt-blocks-accordion-icon-trigger:after, .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basiccircle ):not( .kt-accodion-icon-style-xclosecircle ):not( .kt-accodion-icon-style-arrowcircle ) .kt-accordion-header-wrap .kt-blocks-accordion-header:hover .kt-blocks-accordion-icon-trigger:before, body:not(.hide-focus-outline) .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basiccircle ):not( .kt-accodion-icon-style-xclosecircle ):not( .kt-accodion-icon-style-arrowcircle ) .kt-blocks-accordion--visible .kt-blocks-accordion-icon-trigger:after, body:not(.hide-focus-outline) .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basiccircle ):not( .kt-accodion-icon-style-xclosecircle ):not( .kt-accodion-icon-style-arrowcircle ) .kt-blocks-accordion-header:focus-visible .kt-blocks-accordion-icon-trigger:before{background:#444444;}.kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-accordion-header-wrap .kt-blocks-accordion-header:hover .kt-blocks-accordion-icon-trigger, body:not(.hide-focus-outline) .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-accordion-header-wrap .kt-blocks-accordion-header:focus-visible .kt-blocks-accordion-icon-trigger{background:#444444;}.kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-accordion-header-wrap .kt-blocks-accordion-header:hover .kt-blocks-accordion-icon-trigger:after, .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-accordion-header-wrap .kt-blocks-accordion-header:hover .kt-blocks-accordion-icon-trigger:before, body:not(.hide-focus-outline) .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-accordion-header-wrap .kt-blocks-accordion-header:focus-visible .kt-blocks-accordion-icon-trigger:after, body:not(.hide-focus-outline) .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-accordion-header-wrap .kt-blocks-accordion-header:focus-visible .kt-blocks-accordion-icon-trigger:before{background:#ffffff;}.kt-accordion-id_2eaf18-ec .kt-accordion-header-wrap .kt-blocks-accordion-header:focus-visible,\n\t\t\t\t.kt-accordion-id_2eaf18-ec > .kt-accordion-inner-wrap > .wp-block-kadence-pane > .kt-accordion-header-wrap > .kt-blocks-accordion-header.kt-accordion-panel-active{color:#444444;background:#ffffff;border-top-color:#eeeeee;border-right-color:#eeeeee;border-bottom-color:#eeeeee;border-left-color:#0e9cd1;}.kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basiccircle ):not( .kt-accodion-icon-style-xclosecircle ):not( .kt-accodion-icon-style-arrowcircle )  > .kt-accordion-inner-wrap > .wp-block-kadence-pane > .kt-accordion-header-wrap > .kt-blocks-accordion-header.kt-accordion-panel-active .kt-blocks-accordion-icon-trigger:after, .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basiccircle ):not( .kt-accodion-icon-style-xclosecircle ):not( .kt-accodion-icon-style-arrowcircle )  > .kt-accordion-inner-wrap > .wp-block-kadence-pane > .kt-accordion-header-wrap > .kt-blocks-accordion-header.kt-accordion-panel-active .kt-blocks-accordion-icon-trigger:before{background:#444444;}.kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-blocks-accordion-header.kt-accordion-panel-active .kt-blocks-accordion-icon-trigger{background:#444444;}.kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-blocks-accordion-header.kt-accordion-panel-active .kt-blocks-accordion-icon-trigger:after, .kt-accordion-id_2eaf18-ec:not( .kt-accodion-icon-style-basic ):not( .kt-accodion-icon-style-xclose ):not( .kt-accodion-icon-style-arrow ) .kt-blocks-accordion-header.kt-accordion-panel-active .kt-blocks-accordion-icon-trigger:before{background:#ffffff;}@media all and (max-width: 767px){.kt-accordion-id_2eaf18-ec .kt-accordion-inner-wrap{display:block;}.kt-accordion-id_2eaf18-ec .kt-accordion-inner-wrap .kt-accordion-pane:not(:first-child){margin-top:10px;}}<\/style>\n<div class=\"wp-block-kadence-accordion alignnone\"><div class=\"kt-accordion-wrap kt-accordion-id_2eaf18-ec kt-accordion-has-8-panes kt-active-pane-7 kt-accordion-block kt-pane-header-alignment-left kt-accodion-icon-style-arrow kt-accodion-icon-side-right\" style=\"max-width:none\"><div class=\"kt-accordion-inner-wrap\" data-allow-multiple-open=\"true\" data-start-open=\"none\">\n<div class=\"wp-block-kadence-pane kt-accordion-pane kt-accordion-pane-1 kt-pane_23d2b7-bd\"><div class=\"kt-accordion-header-wrap\"><button class=\"kt-blocks-accordion-header kt-acccordion-button-label-show\" type=\"button\"><span class=\"kt-blocks-accordion-title-wrap\"><span class=\"kt-blocks-accordion-title\">\u26a1 What does [::-1] do in Python?<\/span><\/span><span class=\"kt-blocks-accordion-icon-trigger\"><\/span><\/button><\/div><div class=\"kt-accordion-panel kt-accordion-panel-hidden\"><div class=\"kt-accordion-panel-inner\">\n<p class=\"wp-block-paragraph\">It is a slice with no start, no stop, and step of -1. It returns a new sequence with all elements in reverse order.<\/p>\n<\/div><\/div><\/div>\n\n\n\n<div class=\"wp-block-kadence-pane kt-accordion-pane kt-accordion-pane-2 kt-pane_cce570-61\"><div class=\"kt-accordion-header-wrap\"><button class=\"kt-blocks-accordion-header kt-acccordion-button-label-show\" type=\"button\"><span class=\"kt-blocks-accordion-title-wrap\"><span class=\"kt-blocks-accordion-title\">\ud83e\udd16 How does AI help understand Python slicing?<\/span><\/span><span class=\"kt-blocks-accordion-icon-trigger\"><\/span><\/button><\/div><div class=\"kt-accordion-panel kt-accordion-panel-hidden\"><div class=\"kt-accordion-panel-inner\">\n<p class=\"wp-block-paragraph\">AI assistants like Copilot and ChatGPT explain start, stop, step parameters and convert verbose for-loops to concise slice expressions.<\/p>\n<\/div><\/div><\/div>\n\n\n\n<div class=\"wp-block-kadence-pane kt-accordion-pane kt-accordion-pane-3 kt-pane_abc123-cd\"><div class=\"kt-accordion-header-wrap\"><button class=\"kt-blocks-accordion-header kt-acccordion-button-label-show\" type=\"button\"><span class=\"kt-blocks-accordion-title-wrap\"><span class=\"kt-blocks-accordion-title\">\ud83d\udca1 Can AI suggest readable alternatives to [::-1]?<\/span><\/span><span class=\"kt-blocks-accordion-icon-trigger\"><\/span><\/button><\/div><div class=\"kt-accordion-panel kt-accordion-panel-hidden\"><div class=\"kt-accordion-panel-inner\">\n<p class=\"wp-block-paragraph\">Yes. AI tools recommend list(reversed(seq)) when readability beats brevity, and explain time and memory trade-offs of each.<\/p>\n<\/div><\/div><\/div>\n\n\n\n<div class=\"wp-block-kadence-pane kt-accordion-pane kt-accordion-pane-4 kt-pane_def456-ef\"><div class=\"kt-accordion-header-wrap\"><button class=\"kt-blocks-accordion-header kt-acccordion-button-label-show\" type=\"button\"><span class=\"kt-blocks-accordion-title-wrap\"><span class=\"kt-blocks-accordion-title\">\ud83c\udd9a Difference between a[-1] and a[::-1]?<\/span><\/span><span class=\"kt-blocks-accordion-icon-trigger\"><\/span><\/button><\/div><div class=\"kt-accordion-panel kt-accordion-panel-hidden\"><div class=\"kt-accordion-panel-inner\">\n<p class=\"wp-block-paragraph\">a[-1] returns the last element only. a[::-1] returns the entire sequence reversed.<\/p>\n<\/div><\/div><\/div>\n\n\n\n<div class=\"wp-block-kadence-pane kt-accordion-pane kt-accordion-pane-5 kt-pane_ghi789-gh\"><div class=\"kt-accordion-header-wrap\"><button class=\"kt-blocks-accordion-header kt-acccordion-button-label-show\" type=\"button\"><span class=\"kt-blocks-accordion-title-wrap\"><span class=\"kt-blocks-accordion-title\">\ud83d\udccb Does [::-1] work on lists and strings?<\/span><\/span><span class=\"kt-blocks-accordion-icon-trigger\"><\/span><\/button><\/div><div class=\"kt-accordion-panel kt-accordion-panel-hidden\"><div class=\"kt-accordion-panel-inner\">\n<p class=\"wp-block-paragraph\">Yes. It works on any sequence type including lists, strings, tuples, bytes, and bytearrays. The returned sequence keeps its original type.<\/p>\n<\/div><\/div><\/div>\n\n\n\n<div class=\"wp-block-kadence-pane kt-accordion-pane kt-accordion-pane-6 kt-pane_jkl012-ij\"><div class=\"kt-accordion-header-wrap\"><button class=\"kt-blocks-accordion-header kt-acccordion-button-label-show\" type=\"button\"><span class=\"kt-blocks-accordion-title-wrap\"><span class=\"kt-blocks-accordion-title\">\u26a1 Is [::-1] faster than reversed() in Python?<\/span><\/span><span class=\"kt-blocks-accordion-icon-trigger\"><\/span><\/button><\/div><div class=\"kt-accordion-panel kt-accordion-panel-hidden\"><div class=\"kt-accordion-panel-inner\">\n<p class=\"wp-block-paragraph\">Slicing is generally faster because it runs in C and returns a complete object. reversed() returns a lazy iterator that is memory-friendly but slower.<\/p>\n<\/div><\/div><\/div>\n\n\n\n<div class=\"wp-block-kadence-pane kt-accordion-pane kt-accordion-pane-7 kt-pane_mno345-kl\"><div class=\"kt-accordion-header-wrap\"><button class=\"kt-blocks-accordion-header kt-acccordion-button-label-show\" type=\"button\"><span class=\"kt-blocks-accordion-title-wrap\"><span class=\"kt-blocks-accordion-title\">\ud83d\udee1\ufe0f Does [::-1] modify the original sequence?<\/span><\/span><span class=\"kt-blocks-accordion-icon-trigger\"><\/span><\/button><\/div><div class=\"kt-accordion-panel kt-accordion-panel-hidden\"><div class=\"kt-accordion-panel-inner\">\n<p class=\"wp-block-paragraph\">No. Slicing returns a new sequence. The original list, string, or tuple is left untouched. Use list.reverse() for in-place reversal of lists.<\/p>\n<\/div><\/div><\/div>\n\n\n\n<div class=\"wp-block-kadence-pane kt-accordion-pane kt-accordion-pane-8 kt-pane_pqr678-mn\"><div class=\"kt-accordion-header-wrap\"><button class=\"kt-blocks-accordion-header kt-acccordion-button-label-show\" type=\"button\"><span class=\"kt-blocks-accordion-title-wrap\"><span class=\"kt-blocks-accordion-title\">\ud83d\udcda Can I use [::-1] with NumPy arrays?<\/span><\/span><span class=\"kt-blocks-accordion-icon-trigger\"><\/span><\/button><\/div><div class=\"kt-accordion-panel kt-accordion-panel-hidden\"><div class=\"kt-accordion-panel-inner\">\n<p class=\"wp-block-paragraph\">Yes. NumPy supports the same slice syntax, returning a reversed view rather than a copy. Use .copy() if you need an independent array.<\/p>\n<\/div><\/div><\/div>\n<\/div><\/div><\/div>","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"excerpt":{"rendered":"<p>\u26a1 Smart Summary [::-1] in Python is a powerful slice expression that reverses any sequence type. Lessons cover indexing fundamentals, slice parameters, the difference between a[-1] and a[::-1], list reversal, and worked examples. \ud83d\udd01 Reverse Slice: [::-1] uses empty start, empty stop, step -1. \ud83d\udccd Indexing: Positive indices start at zero; negative count from the&#8230;<\/p>\n","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"author":60,"featured_media":169229,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[40],"tags":[154],"coauthors":[501],"class_list":["post-25772","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-convertbox-developer"],"taxonomy_info":{"category":[{"value":40,"label":"Python"}],"post_tag":[{"value":154,"label":"Convertbox-Developer"}]},"featured_image_src_large":["https:\/\/www.guru99.com\/images\/python-slice-reverse-with-examples.png",700,250,false],"author_info":{"display_name":"Anna Blake","author_link":"https:\/\/www.guru99.com\/author\/anna"},"comment_info":0,"category_info":[{"term_id":40,"name":"Python","slug":"python","term_group":0,"term_taxonomy_id":40,"taxonomy":"category","description":"","parent":0,"count":101,"filter":"raw","cat_ID":40,"category_count":101,"category_description":"","cat_name":"Python","category_nicename":"python","category_parent":0}],"tag_info":[{"term_id":154,"name":"Convertbox-Developer","slug":"convertbox-developer","term_group":0,"term_taxonomy_id":154,"taxonomy":"post_tag","description":"","parent":0,"count":823,"filter":"raw"}],"gt_translate_keys":[{"key":"link","format":"url"}],"_links":{"self":[{"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/posts\/25772","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/users\/60"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/comments?post=25772"}],"version-history":[{"count":2,"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/posts\/25772\/revisions"}],"predecessor-version":[{"id":169363,"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/posts\/25772\/revisions\/169363"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/media\/169229"}],"wp:attachment":[{"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/media?parent=25772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/categories?post=25772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/tags?post=25772"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.guru99.com\/wp-json\/wp\/v2\/coauthors?post=25772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}