[Tut] 6 Ways to Remove Python List Elements - 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] 6 Ways to Remove Python List Elements (/thread-100149.html) |
[Tut] 6 Ways to Remove Python List Elements - xSicKxBot - 10-27-2022 6 Ways to Remove Python List Elements <div> <div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{"align":"left","id":"830190","slug":"default","valign":"top","ignore":"","reference":"auto","class":"","count":"2","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (2 votes)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}"> <div class="kksr-stars"> <div class="kksr-stars-inactive"> <div class="kksr-star" data-star="1" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="2" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="3" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="4" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="5" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> <div class="kksr-stars-active" style="width: 142.5px;"> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> </div> <div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (2 votes) </div> </div> <h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Problem Formulation and Solution Overview</h2> <p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">This article will show you how 6 ways to remove <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> elements in Python.</p> <p>To make it more interesting, we have the following running scenario:</p> <p class="wp-embed-aspect-16-9 wp-has-aspect-ratio"><em>Suppose you have a Christmas <em><a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a></em></em> <em>containing everyone to buy a gift for. Once a gift is purchased, remove this person from the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a>. Once all gifts have been purchased, remove the entire <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a>.</em></p> <div class="wp-block-image"> <figure class="aligncenter size-full is-resized"><img loading="lazy" src="https://blog.finxter.com/wp-content/uploads/2022/10/xmas_lst_02.png" alt="" class="wp-image-837170" width="609" height="159" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/xmas_lst_02.png 900w, https://blog.finxter.com/wp-content/uploads/2022/10/xmas_lst_02-300x79.png 300w, https://blog.finxter.com/wp-content/uploads/2022/10/xmas_lst_02-768x201.png 768w" sizes="(max-width: 609px) 100vw, 609px" /></figure> </div> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code>xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']</code></td> </tr> </tbody> </table> </figure> <hr class="wp-block-separator has-alpha-channel-opacity wp-embed-aspect-16-9 wp-has-aspect-ratio" /> <p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: How would we write code to remove items from a Python <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a><em>?</em></p> <p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">We can accomplish this task by one of the following options:</p> <ul type="video" class="wp-embed-aspect-16-9 wp-has-aspect-ratio"> <li><strong>Method </strong>1: Use the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-cheat-sheet/" data-type="URL" data-id="https://blog.finxter.com/python-cheat-sheet/" target="_blank"><code>del</code></a> Keyword</li> <li><strong>Method </strong>2: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-set-remove/" data-type="URL" data-id="https://blog.finxter.com/python-set-remove/" target="_blank"><code>remove()</code></a> and a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank"><code>for</code></a> Loop</li> <li><strong>Method </strong>3: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/introduction-to-slicing-in-python/" data-type="URL" data-id="https://blog.finxter.com/introduction-to-slicing-in-python/" target="_blank">slicing</a></li> <li><strong>Method 4</strong>: Use <a href="https://blog.finxter.com/python-list-pop/" data-type="URL" data-id="https://blog.finxter.com/python-list-pop/"><code>pop()</code></a></li> <li><strong>Method 5</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank">List Comprehension</a></li> <li><strong>Method 6</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-clear/" data-type="URL" data-id="https://blog.finxter.com/python-list-clear/" target="_blank"><code>clear()</code></a></li> </ul> <hr class="wp-block-separator has-alpha-channel-opacity" /> <h2>Method 1: Use the del Keyword</h2> <p class="has-global-color-8-background-color has-background">This method uses Python’s <a rel="noreferrer noopener" href="https://blog.finxter.com/python-cheat-sheet/" data-type="URL" data-id="https://blog.finxter.com/python-cheat-sheet/" target="_blank"><code>del</code></a> Keyword and highlights its ability to remove one <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> element and all <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> elements. </p> <p><span style="text-decoration: underline">Remove One List Element</span></p> <p>In this scenario, <code>Asa's</code> gift has been purchased and will be removed from the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a>. </p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn'] del xmas_list[3] print(xmas_list)</pre> <p>As shown on the highlighted line, <code>Asa</code> is removed from the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> by using <a rel="noreferrer noopener" href="https://blog.finxter.com/python-cheat-sheet/" data-type="URL" data-id="https://blog.finxter.com/python-cheat-sheet/" target="_blank"><code>del</code></a>, referencing <code>xmas_list</code> and specifying Asa’s location (<code>[3]</code>).</p> <p>When <code>xmas_list</code> is output to the terminal, the following displays.</p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code>['Anna', 'Elin', 'Inger', 'Sofie', 'Gunnel', 'Linn']</code></td> </tr> </tbody> </table> </figure> <p><span style="text-decoration: underline">Remove All List Elements</span></p> <p>In this scenario, all gifts have been purchased, and all <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> elements will be removed.</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn'] del xmas_list print(xmas_list)</pre> <p>As shown on the highlighted line, all elements of <code>xmas_list</code> are removed by using <a rel="noreferrer noopener" href="https://blog.finxter.com/python-cheat-sheet/" data-type="URL" data-id="https://blog.finxter.com/python-cheat-sheet/" target="_blank"><code>del</code></a> and referencing <code>xmas_list</code>. </p> <p>When <code>xmas_list</code> is output to the terminal, the following error is generated.</p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code>NameError: name 'xmas_list' is not defined</code></td> </tr> </tbody> </table> </figure> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Note</strong>: This error is generated because the variable <code>xmas_list</code> no longer exists in memory.</p> <figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/6-ways-to-remove-python-list-elements/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FqXOFngx0bQU%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure> <hr class="wp-block-separator has-alpha-channel-opacity" /> <h2>Method 2: Use remove() and a For Loop</h2> <p class="has-global-color-8-background-color has-background">This example uses the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-set-remove/" data-type="URL" data-id="https://blog.finxter.com/python-set-remove/" target="_blank"><code>remove()</code></a> function in conjunction with a <a href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/"><code>for</code></a> loop to remove one <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> element and all <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> elements. </p> <p><span style="text-decoration: underline">Remove One List Element</span></p> <p>In this scenario, <code><code>Elin's</code></code> gift has been purchased and will be removed from the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank">List</a>.</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn'] xmas_list.remove('Elin') print(xmas_list)</pre> <p>As shown on the highlighted line, <code>Elin</code> is removed from the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> using the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-set-remove/" data-type="URL" data-id="https://blog.finxter.com/python-set-remove/" target="_blank"><code>remove()</code></a> function and passing Elin’s name as an argument. </p> <p>When <code>xmas_list</code> is output to the terminal, the following displays.</p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code>['Anna', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']</code></td> </tr> </tbody> </table> </figure> <p><span style="text-decoration: underline">Remove All List Elements</span></p> <p>In this scenario, all gifts have been purchased, and all <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> elements will be removed.</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2-3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn'] for item in xmas_list.copy(): xmas_list.remove(item) print(xmas_list)</pre> <p>As shown on the first highlighted line, a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank"><code>for</code></a> loop is instantiated. This loop declares a shallow copy of the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> to iterate. </p> <p>On each iteration, the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-set-remove/" data-type="URL" data-id="https://blog.finxter.com/python-set-remove/" target="_blank"><code>remove()</code></a> function is called and passed the current name in <code>xmas_list</code> as an argument (see below) and removed. </p> <p>For example:</p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code>Anna<br />Elin<br />Inger<br />Asa<br />Sofie<br />Gunnel<br />Linn</code></td> </tr> </tbody> </table> </figure> <p>When <code>xmas_list</code> is output to the terminal, an empty <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> displays.</p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code>[]</code></td> </tr> </tbody> </table> </figure> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Note</strong>: A shallow copy creates a reference to the original <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a>. For further details, view the video below.</p> <figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/6-ways-to-remove-python-list-elements/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVhLrz7ivZbc%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure> <hr class="wp-block-separator has-alpha-channel-opacity" /> <h2>Method 3: Use slicing</h2> <p class="has-global-color-8-background-color has-background">This method uses <a rel="noreferrer noopener" href="https://blog.finxter.com/introduction-to-slicing-in-python/" data-type="URL" data-id="https://blog.finxter.com/introduction-to-slicing-in-python/" target="_blank">slicing</a> to remove one <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank">List</a> element and all <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> elements. </p> <p><span style="text-decoration: underline">Remove One List Element</span></p> <p>In this scenario, <code><code>Anna<code>'s</code></code></code> gift has been purchased and will be removed from the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank">List</a>.</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn'] xmas_list = xmas_list[1:] print(xmas_list)</pre> <p>As shown on the highlighted line, <code>Anna</code> is removed from the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> using <a rel="noreferrer noopener" href="https://blog.finxter.com/introduction-to-slicing-in-python/" data-type="URL" data-id="https://blog.finxter.com/introduction-to-slicing-in-python/" target="_blank"><code>slicing</code></a>. </p> <p>When <code>xmas_list</code> is output to the terminal, the following displays.</p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code>['Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']</code></td> </tr> </tbody> </table> </figure> <p><span style="text-decoration: underline">Remove All List Elements</span></p> <p>In this scenario, all gifts have been purchased, and all <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> elements will be removed.</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn'] xmas_list = [] print(xmas_list)</pre> <p>As shown on the highlighted line, all <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> elements are removed by declaring an empty <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a>. </p> <p>When <code>xmas_list</code> is output to the terminal, an empty <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> displays.</p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code>[]</code></td> </tr> </tbody> </table> </figure> <figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/6-ways-to-remove-python-list-elements/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FD2ZueuWXST8%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure> <hr class="wp-block-separator has-alpha-channel-opacity" /> <h2>Method 4: Use pop()</h2> <p class="has-global-color-8-background-color has-background">This method uses the <a href="https://blog.finxter.com/python-list-pop/" data-type="URL" data-id="https://blog.finxter.com/python-list-pop/"><code>pop()</code></a> function to remove one <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank">List</a> element and all <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> elements. </p> <p><span style="text-decoration: underline">Remove One List Element</span></p> <p>In this scenario, <code><code>Linn'<code>s</code></code></code> gift has been purchased and will be removed from the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a>.</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2-3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn'] xmas_list.pop() xmas_list.pop(2) print(xmas_list)</pre> <p>As shown on the first highlighted line, the <a href="https://blog.finxter.com/python-list-pop/" data-type="URL" data-id="https://blog.finxter.com/python-list-pop/"><code>pop()</code></a> method is appended to the <code>xmas_list</code>. This lets Python know to remove a List element from said <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank">List</a>. Since no element is specified, the last element is removed (<code>Linn</code>).</p> <p>On the second highlighted line, the <a href="https://blog.finxter.com/python-list-pop/" data-type="URL" data-id="https://blog.finxter.com/python-list-pop/"><code>pop()</code></a> method is appended to the <code>xmas_list</code> and passed one (1) argument: the element to remove (<code>2</code>). This action removes Inger.</p> <p>When <code>xmas_list</code> is output to the terminal, the following displays.</p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code>['Anna', 'Elin', 'Asa', 'Sofie', 'Gunnel']</code></td> </tr> </tbody> </table> </figure> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Note</strong>: Both Linn and Inger are no longer in <code>xmas_list</code>.</p> <p><span style="text-decoration: underline">Remove All List Elements</span></p> <p>In this scenario, all gifts have been purchased, and all <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> elements will be removed.</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3-4" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn'] for i in xmas_list.copy(): xmas_list.pop() print(xmas_list)</pre> <p>As shown on the first highlighted line, a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank"><code>for</code></a> loop is instantiated. This loop declares a shallow copy of the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> to iterate. </p> <p>On each iteration, the <a href="https://blog.finxter.com/python-list-pop/" data-type="URL" data-id="https://blog.finxter.com/python-list-pop/"><code>pop()</code></a> method is called. Since no argument is passed, the last element is removed.</p> <p>When <code>xmas_list</code> is output to the terminal, an empty <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> displays.</p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code>[]</code></td> </tr> </tbody> </table> </figure> <figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/6-ways-to-remove-python-list-elements/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fr9VhOWN5oEg%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure> <hr class="wp-block-separator has-alpha-channel-opacity" /> <h2>Method 5: Use List Comprehension</h2> <p class="has-global-color-8-background-color has-background">This method uses <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" target="_blank">List Comprehension</a> to remove all <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank">List</a> elements that do not meet the specified criteria.</p> <p><span style="text-decoration: underline">Remove One List Element</span></p> <p>In this scenario, <code><code>Gunnel's </code></code>gift has been purchased and will be removed from the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank">List</a>.</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn'] xmas_list = [value for value in xmas_list if value != 'Gunnel'] print(xmas_list)</pre> <p>When <code>xmas_list</code> is output to the terminal, the following displays.</p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code>['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Linn']</code></td> </tr> </tbody> </table> </figure> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Note</strong>: To remove all List elements, pass it empty brackets as shown follows: (<code>xmas_list = []</code>).</p> <figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/6-ways-to-remove-python-list-elements/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FaBC0VhpXkOQ%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure> <hr class="wp-block-separator has-alpha-channel-opacity" /> <h2>Method 6: Use clear()</h2> <p class="has-global-color-8-background-color has-background">This method uses <a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-clear/" target="_blank"><code>clear()</code></a> to remove all <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank">List</a> elements.</p> <p>In this scenario, all gifts have been purchased, and all <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> elements will be removed.</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn'] xmas_list.clear() print(xmas_list)</pre> <p>As shown on the highlighted line, all <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> elements are removed by appending the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-clear/" target="_blank"><code>clear()</code></a> function to <code>xmas_list</code>.</p> <p>When <code>xmas_list</code> is output to the terminal, an empty <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> displays.</p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code>[]</code></td> </tr> </tbody> </table> </figure> <figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/6-ways-to-remove-python-list-elements/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FfqscJGzsDKc%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure> <hr class="wp-block-separator has-alpha-channel-opacity" /> <h2>Summary</h2> <p>This article has provided six (6) ways to remove <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank">List</a> elements to select the best fit for your coding requirements.</p> <p>Good Luck & Happy Coding!</p> <hr class="wp-block-separator has-alpha-channel-opacity" /> <h2>Programming Humor</h2> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Programming is 10% science, 20% ingenuity, and 70% getting the ingenuity to work with the science.</p> <p>~~~</p> <ul class="has-global-color-8-background-color has-background"> <li><strong>Question</strong>: Why do Java programmers wear glasses?</li> <li><strong>Answer</strong>: Because they cannot C# …!</li> </ul> <p>Feel free to check out <a href="https://blog.finxter.com/what-is-the-geekiest-joke/" data-type="post" data-id="617" target="_blank" rel="noreferrer noopener">our blog article</a> with more coding jokes. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f609.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p> </div> https://www.sickgaming.net/blog/2022/10/26/6-ways-to-remove-python-list-elements/ |