{"id":3257,"date":"2026-11-30T11:42:25","date_gmt":"2026-11-30T11:42:25","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3257"},"modified":"2026-11-30T11:42:25","modified_gmt":"2026-11-30T11:42:25","slug":"python-list-clear","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-list-clear\/","title":{"rendered":"Python List clear() Method with Examples (clear vs [] vs del)"},"content":{"rendered":"<p>The <code>clear()<\/code> method removes <strong>all<\/strong> items from a list, leaving it empty. The list itself still exists, so you can keep adding to it. This page explains <code>clear()<\/code> with outputs, shows the other ways to empty a list, and explains the one difference that really matters: <code>clear()<\/code> vs assigning <code>[]<\/code>.<\/p>\n<h2>Syntax<\/h2>\n<pre class=\"wp-block-code\"><code>list.clear()<\/code><\/pre>\n<p>It takes no arguments and returns <code>None<\/code>.<\/p>\n<h2>Basic Example<\/h2>\n<pre class=\"wp-block-code\"><code>nums = [10, 20, 30, 40]\nnums.clear()\nprint(nums, len(nums))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>[] 0<\/code><\/pre>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-clear-METHOD-IN-PYTHON.jpg\" alt=\"Python list clear method example\" \/><\/figure>\n<h2>clear() vs list = []<\/h2>\n<p>Both make a list look empty, but they work differently when another variable points to the same list:<\/p>\n<pre class=\"wp-block-code\"><code>a = [1, 2, 3]\nb = a          # b refers to the SAME list\na.clear()\nprint(a, b)\n\na = [1, 2, 3]\nb = a\na = []         # a now refers to a NEW empty list\nprint(a, b)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>[] []\n[] [1, 2, 3]<\/code><\/pre>\n<p><code>clear()<\/code> empties the list object itself, so every variable that refers to it sees the change. <code>a = []<\/code> only points <code>a<\/code> to a brand-new list; the old list, still referenced by <code>b<\/code>, is untouched. Use <code>clear()<\/code> when other parts of your program share the list and should all see it emptied.<\/p>\n<h2>Other Ways to Empty a List<\/h2>\n<pre class=\"wp-block-code\"><code>c = [1, 2, 3]\ndel c[:]       # delete every item using a slice\nprint(c)\n\nd = [1, 2, 3]\nd *= 0         # repeat the list zero times\nprint(d)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>[]\n[]<\/code><\/pre>\n<p>Both work in place like <code>clear()<\/code>, but <code>clear()<\/code> is the clearest to read.<\/p>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-clear-METHOD-IN-PYTHON-1-1.jpg\" alt=\"Emptying a Python list using del\" \/><\/figure>\n<h2>Do Not Confuse clear() with del list<\/h2>\n<pre class=\"wp-block-code\"><code>e = [1, 2, 3]\ndel e\nprint(e)<\/code><\/pre>\n<pre class=\"wp-block-code\"><code>NameError: name 'e' is not defined<\/code><\/pre>\n<p><code>del e<\/code> deletes the variable itself, not just its contents. After it, the name <code>e<\/code> no longer exists.<\/p>\n<h2>clear() Returns None<\/h2>\n<pre class=\"wp-block-code\"><code>print([5, 6].clear())<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>None<\/code><\/pre>\n<p>So never write <code>my_list = my_list.clear()<\/code>; that would set <code>my_list<\/code> to <code>None<\/code>.<\/p>\n<h2>clear() on Other Collections<\/h2>\n<pre class=\"wp-block-code\"><code>cart = {'pen': 2}\ncart.clear()\nprint(cart)\n\ns = {1, 2}\ns.clear()\nprint(s)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{}\nset()<\/code><\/pre>\n<p>Dictionaries and sets also have a <code>clear()<\/code> method. Tuples and strings do not, because they are immutable.<\/p>\n<h2>Frequently Asked Questions<\/h2>\n<h3>What does list.clear() return?<\/h3>\n<p><code>None<\/code>. It empties the list in place.<\/p>\n<h3>What is the difference between clear() and del?<\/h3>\n<p><code>clear()<\/code> removes all items but keeps the list; <code>del my_list<\/code> removes the variable completely.<\/p>\n<h3>Is clear() available in Python 2?<\/h3>\n<p>No, <code>list.clear()<\/code> was added in Python 3.3. In older code you may see <code>del my_list[:]<\/code> instead.<\/p>\n<p>Related: <a href=\"https:\/\/gyanipandit.com\/programming\/python-list-sort\/\">Python list sort()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/accessing-tuple-elements-python\/\">Accessing tuple elements<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The clear() method removes all items from a list, leaving it empty. The list itself still exists, so you can keep adding to it. This page explains clear() with outputs, shows the other ways to empty a list, and explains the one difference that really matters: clear() vs assigning []. Syntax list.clear() It takes no &#8230; <a title=\"Python List clear() Method with Examples (clear vs [] vs del)\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/python-list-clear\/\" aria-label=\"Read more about Python List clear() Method with Examples (clear vs [] vs del)\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":["post-3257","post","type-post","status-publish","format-standard","hentry","category-python-tutorial","tag-python","tag-python-tutorial"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3257","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3257"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3257\/revisions"}],"predecessor-version":[{"id":6169,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3257\/revisions\/6169"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}