Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] list.clear() vs New List — Why Clearing a List Rather Than Creating a New One?

#1
list.clear() vs New List — Why Clearing a List Rather Than Creating a New One?

<div><p><strong>Problem</strong>: You’ve just learned about the <code><a href="https://blog.finxter.com/python-list-clear/" target="_blank" rel="noreferrer noopener" title="Python List clear()">list.clear()</a></code> method in Python. You wonder, what’s its purpose? Why not creating a new list and overwriting the variable instead of clearing an existing list?</p>
<p><strong>Example</strong>: Say, you have 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="">lst = ['Alice', 'Bob', 'Carl']</pre>
<p>If you clear the list, it becomes empty:</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="">lst.clear()
print(lst)
# []</pre>
<p>However, you could have accomplished the same thing by just assigning a new empty list to the variable <code>lst</code>:</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="">lst = ['Alice', 'Bob', 'Carl']
lst = []
print(lst)
# []
</pre>
<p>The output is the same. Why does the <code>list.clear()</code> method exist in the first place?</p>
<p>If you go through the following interactive memory visualizer, you’ll see that both variants lead to different results if you have multiple variables pointing to the list object: </p>
<p> <iframe width="800" height="500" frameborder="0" src="https://pythontutor.com/iframe-embed.html#code=lst_1%20%3D%20%5B'Alice',%20'Bob',%20'Carl'%5D%0Alst_2%20%3D%20lst_1%0Alst_1.clear%28%29%0A%0A%0A%0Alst_1%20%3D%20%5B'Alice',%20'Bob',%20'Carl'%5D%0Alst_2%20%3D%20lst_1%0Alst_1%20%3D%20%5B%5D%0A&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=1&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe> </p>
<p>In the second example, the variable <code>lst_2</code> still points to a non-empty list object!</p>
<p>So, there are at least two reasons why the <code>list.clear()</code> method can be superior to <a href="https://blog.finxter.com/how-to-create-a-python-list/" title="How to Create a Python List?" target="_blank" rel="noreferrer noopener">creating a new list</a>:</p>
<ul>
<li><strong>Release Memory: </strong>If you have a large list that fills your memory—such as a huge data set or a large file read via <code><a href="https://blog.finxter.com/how-to-read-a-file-line-by-line-and-store-into-a-list/" title="How to Read a File Line-By-Line and Store Into a List?" target="_blank" rel="noreferrer noopener">readlines()</a></code>—and you don’t need it anymore, you can immediately release the memory with <code>list.clear()</code>. Especially in interactive mode, Python doesn’t know which variable you still need – so it must keep all variables till session end. But if you call <code>list.clear()</code>, it can release the memory for other processing tasks.</li>
<li><strong>Clear Multiple List Variables: </strong>Multiple variables may refer to the same list object. If you want to reflect that the list is now empty, you can either call <code>list.clear()</code> on one variable and all other variables will see it, or you must call <code>var1 = [], var2 = [], ..., varn = []</code> for all variables. This can be a pain if you have many variables.</li>
</ul>
<p>Do you want to develop the skills of a <strong>well-rounded Python professional</strong>—while getting paid in the process? Become a Python freelancer and order your book <a href="https://amzn.to/2Re2JqO" target="_blank" rel="noreferrer noopener"><strong>Leaving the Rat Race with Python</strong></a> on Amazon (<em>Kindle/Print</em>)!</p>
<div class="wp-block-image">
<figure class="aligncenter size-medium is-resized"><a href="https://amzn.to/2Re2JqO" target="_blank" rel="noopener noreferrer"><img loading="lazy" src="https://blog.finxter.com/wp-content/uploads/2020/08/final_cover-200x300.jpg" alt="Leaving the Rat Race with Python Book" class="wp-image-11850" width="200" height="300" srcset="https://blog.finxter.com/wp-content/uploads/2020/08/final_cover-200x300.jpg 200w, https://blog.finxter.com/wp-content/uplo...scaled.jpg 683w, https://blog.finxter.com/wp-content/uplo...8x1152.jpg 768w, https://blog.finxter.com/wp-content/uplo...4x1536.jpg 1024w, https://blog.finxter.com/wp-content/uplo...5x2048.jpg 1365w, https://blog.finxter.com/wp-content/uplo...50x225.jpg 150w" sizes="(max-width: 200px) 100vw, 200px" /></a></figure>
</div>
<p>The post <a href="https://blog.finxter.com/list-clear-vs-new-list/" target="_blank" rel="noopener noreferrer">list.clear() vs New List — Why Clearing a List Rather Than Creating a New One?</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/11/...a-new-one/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016