Sick Gaming
[Tut] Python Reverse List with Slicing — An Illustrated Guide - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] Python Reverse List with Slicing — An Illustrated Guide (/thread-97543.html)



[Tut] Python Reverse List with Slicing — An Illustrated Guide - xSicKxBot - 10-02-2020

Python Reverse List with Slicing — An Illustrated Guide

<div><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>
<div class="wp-block-image">
<figure class="aligncenter size-large is-resized"><img 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="(max-width: 768px) 100vw, 768px" /></figure>
</div>
<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>
<p><strong>Example</strong>: Say, you’ve got the following list:</p>
<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>
<p>Your goal is to reverse the elements to obtain the following result:</p>
<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>
<h2>Slicing with Default Start and Stop Values</h2>
<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>
<p>Use slicing notation <code>s[startConfusedtopConfusedtep]</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>
<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>
<p>You can use a negative step size (e.g., -1) to slice from the right to the left in inverse order. Here’s how you can use this to reverse a list in Python:</p>
<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
names = ['Alice', 'Bob', 'Carl', 'Dora']
names = names[::-1]
print(names)
# ['Dora', 'Carl', 'Bob', 'Alice']</pre>
<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">“Coffee Break Python Slicing”</a> that will make you a slice pro in no time! </p>
<h2>Alternatives Reversing List</h2>
<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>
<ul>
<li><code>list.reverse()</code> — Best if you want to <strong>reverse the elements of list in place</strong>.</li>
<li><code>list[::-1]</code> — Best if you want to write <strong>concise code to return a new list </strong>with reversed elements.</li>
<li><code>reversed(list)</code> — Best if you want to <strong>iterate over all elements</strong> of a list in reversed order without changing the original list.</li>
</ul>
<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>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe 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>
</div>
</figure>
<p>Try it yourself in our interactive Python shell:</p>
<p> <iframe src="https://trinket.io/embed/python/c6fcdd41c9" marginwidth="0" marginheight="0" allowfullscreen="" width="100%" height="600" frameborder="0"></iframe> </p>
<p><em><strong>Exercise</strong>: Run the code. Do all methods result in the same reversed list?</em></p>
<h2 class="wp-block-block">Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<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>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
<p>The post <a href="https://blog.finxter.com/python-reverse-list-with-slicing/" target="_blank" rel="noopener noreferrer">Python Reverse List with Slicing — An Illustrated Guide</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p>
</div>


https://www.sickgaming.net/blog/2020/10/01/python-reverse-list-with-slicing-an-illustrated-guide/