{"id":121366,"date":"2020-11-30T15:50:35","date_gmt":"2020-11-30T15:50:35","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=17623"},"modified":"2020-11-30T15:50:35","modified_gmt":"2020-11-30T15:50:35","slug":"list-clear-vs-new-list-why-clearing-a-list-rather-than-creating-a-new-one","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/11\/30\/list-clear-vs-new-list-why-clearing-a-list-rather-than-creating-a-new-one\/","title":{"rendered":"list.clear() vs New List \u2014 Why Clearing a List Rather Than Creating a New One?"},"content":{"rendered":"<p><strong>Problem<\/strong>: You&#8217;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&#8217;s its purpose? Why not creating a new list and overwriting the variable instead of clearing an existing list?<\/p>\n<p><strong>Example<\/strong>: Say, you have 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=\"\">lst = ['Alice', 'Bob', 'Carl']<\/pre>\n<p>If you clear the list, it becomes empty:<\/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=\"\">lst.clear()\nprint(lst)\n# []<\/pre>\n<p>However, you could have accomplished the same thing by just assigning a new empty list to the variable <code>lst<\/code>:<\/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=\"\">lst = ['Alice', 'Bob', 'Carl']\nlst = []\nprint(lst)\n# []\n<\/pre>\n<p>The output is the same. Why does the <code>list.clear()<\/code> method exist in the first place?<\/p>\n<p>If you go through the following interactive memory visualizer, you&#8217;ll see that both variants lead to different results if you have multiple variables pointing to the list object: <\/p>\n<p> <iframe loading=\"lazy\" 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&#038;codeDivHeight=400&#038;codeDivWidth=350&#038;cumulative=false&#038;curInstr=1&#038;heapPrimitives=nevernest&#038;origin=opt-frontend.js&#038;py=3&#038;rawInputLstJSON=%5B%5D&#038;textReferences=false\"> <\/iframe> <\/p>\n<p>In the second example, the variable <code>lst_2<\/code> still points to a non-empty list object!<\/p>\n<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>\n<ul>\n<li><strong>Release Memory: <\/strong>If you have a large list that fills your memory&#8212;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>&#8212;and you don&#8217;t need it anymore, you can immediately release the memory with <code>list.clear()<\/code>. Especially in interactive mode, Python doesn&#8217;t know which variable you still need &#8211; 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>\n<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>\n<\/ul>\n<p>Do you want to develop the skills of a <strong>well-rounded Python professional<\/strong>&#8212;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>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-medium is-resized\"><a href=\"https:\/\/amzn.to\/2Re2JqO\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" 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\/uploads\/2020\/08\/final_cover-scaled.jpg 683w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/08\/final_cover-768x1152.jpg 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/08\/final_cover-1024x1536.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/08\/final_cover-1365x2048.jpg 1365w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/08\/final_cover-150x225.jpg 150w\" sizes=\"auto, (max-width: 200px) 100vw, 200px\" \/><\/a><\/figure>\n<\/div>\n<p>The post <a href=\"https:\/\/blog.finxter.com\/list-clear-vs-new-list\/\" target=\"_blank\" rel=\"noopener noreferrer\">list.clear() vs New List &#8212; 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>\n","protected":false},"excerpt":{"rendered":"<p>Problem: You&#8217;ve just learned about the list.clear() method in Python. You wonder, what&#8217;s its purpose? Why not creating a new list and overwriting the variable instead of clearing an existing list? Example: Say, you have the following list. lst = [&#8216;Alice&#8217;, &#8216;Bob&#8217;, &#8216;Carl&#8217;] If you clear the list, it becomes empty: lst.clear() print(lst) # [] [&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-121366","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\/121366","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=121366"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/121366\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=121366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=121366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=121366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}