{"id":118758,"date":"2020-10-01T06:06:52","date_gmt":"2020-10-01T06:06:52","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=14044"},"modified":"2020-10-01T06:06:52","modified_gmt":"2020-10-01T06:06:52","slug":"python-reverse-list-with-slicing-an-illustrated-guide","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/10\/01\/python-reverse-list-with-slicing-an-illustrated-guide\/","title":{"rendered":"Python Reverse List with Slicing \u2014 An Illustrated Guide"},"content":{"rendered":"<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>Summary<\/strong>: The slice notation <code>list[::-1]<\/code> with default <code>start<\/code> and <code>stop<\/code> indices and negative step size <code>-1<\/code> reverses a given <code>list<\/code>. <\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/10\/blogMostPythonicWay-1024x576.jpg\" alt=\"Python Reverse List with Slicing\" class=\"wp-image-14055\" width=\"768\" height=\"432\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/10\/blogMostPythonicWay-scaled.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/10\/blogMostPythonicWay-300x169.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/10\/blogMostPythonicWay-768x432.jpg 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/10\/blogMostPythonicWay-150x84.jpg 150w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/figure>\n<\/div>\n<p><strong>Problem<\/strong>: Given a <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" title=\"The Ultimate Guide to Python Lists\" target=\"_blank\" rel=\"noreferrer noopener\">list of elements<\/a>. How to reverse the order of the elements in the list.<\/p>\n<p><strong>Example<\/strong>: Say, you\u2019ve got the following list:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">['Alice', 'Bob', 'Carl', 'Dora']<\/pre>\n<p>Your goal is to reverse the elements to obtain the following result:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">['Dora', 'Carl', 'Bob', 'Alice']<\/pre>\n<h2>Slicing with Default Start and Stop Values<\/h2>\n<p><a href=\"https:\/\/blog.finxter.com\/introduction-to-slicing-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">Slicing <\/a>is a concept to carve out a substring from a given string.<\/p>\n<p>Use slicing notation <code>s[start:stop:step]<\/code> to access every <code>step<\/code>-th element starting from index <code>start<\/code> (included) and ending in index <code>stop<\/code> (excluded).<\/p>\n<p>All three arguments are optional, so you can skip them to use the default values (<code>start=0<\/code>, <code>stop=len(lst)<\/code>, <code>step=1<\/code>). For example, the expression <code>s[2:4]<\/code> from string <code>'hello'<\/code> carves out the slice <code>'ll'<\/code> and the expression <code>s[:3:2]<\/code> carves out the slice <code>'hl'<\/code>. Note that slicing works the same for lists and strings. <\/p>\n<p>You can use a negative step size (e.g., -1) to slice from the right to the left in inverse order. Here\u2019s how you can use this to reverse a list in Python:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Reverse a List with Slicing\nnames = ['Alice', 'Bob', 'Carl', 'Dora']\nnames = names[::-1]\nprint(names)\n# ['Dora', 'Carl', 'Bob', 'Alice']<\/pre>\n<p>Python masters know slicing from the inside out. Do you want to improve your slicing skills? Check out my book <a href=\"https:\/\/amzn.to\/39nFdzX\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/amzn.to\/39nFdzX\">&#8220;Coffee Break Python Slicing&#8221;<\/a> that will make you a slice pro in no time! <\/p>\n<h2>Alternatives Reversing List<\/h2>\n<p>Alternatively, you can also use other methods to <a href=\"https:\/\/blog.finxter.com\/how-to-reverse-a-list-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"How to Reverse a List in Python?\">reverse a list<\/a>.<\/p>\n<ul>\n<li><code>list.reverse()<\/code> &#8212; Best if you want to <strong>reverse the elements of list in place<\/strong>.<\/li>\n<li><code>list[::-1]<\/code> &#8212; Best if you want to write <strong>concise code to return a new list <\/strong>with reversed elements.<\/li>\n<li><code>reversed(list)<\/code> &#8212; Best if you want to <strong>iterate over all elements<\/strong> of a list in reversed order without changing the original list.<\/li>\n<\/ul>\n<p>The method <code>list.reverse()<\/code> can be 37% faster than <code>reversed(list)<\/code> because no new object has to be created.<\/p>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"How to Reverse a List in Python?\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/OSkQYTlvOXw?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p>Try it yourself in our interactive Python shell:<\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/c6fcdd41c9\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen=\"\" width=\"100%\" height=\"600\" frameborder=\"0\"><\/iframe> <\/p>\n<p><em><strong>Exercise<\/strong>: Run the code. Do all methods result in the same reversed list?<\/em><\/p>\n<h2 class=\"wp-block-block\">Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?<\/p>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n<p>The post <a href=\"https:\/\/blog.finxter.com\/python-reverse-list-with-slicing\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python Reverse List with Slicing &#8212; An Illustrated Guide<\/a> first appeared on <a href=\"https:\/\/blog.finxter.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Finxter<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: The slice notation list[::-1] with default start and stop indices and negative step size -1 reverses a given list. Problem: Given a list of elements. How to reverse the order of the elements in the list. Example: Say, you\u2019ve got the following list: [&#8216;Alice&#8217;, &#8216;Bob&#8217;, &#8216;Carl&#8217;, &#8216;Dora&#8217;] Your goal is to reverse the elements [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-118758","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/118758","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=118758"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/118758\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=118758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=118758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=118758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}