{"id":3301,"date":"2023-03-15T20:00:00","date_gmt":"2023-03-15T11:00:00","guid":{"rendered":"https:\/\/python-academia.com\/en\/?p=3301"},"modified":"2023-01-15T00:24:59","modified_gmt":"2023-01-14T15:24:59","slug":"list-empty","status":"publish","type":"post","link":"https:\/\/python-academia.com\/en\/list-empty\/","title":{"rendered":"[Python]How to Check if a List is Empty, and How to Create a Empty List"},"content":{"rendered":"\n<p>This article shows <span class=\"st-mymarker-s\">how to check if a list is empty<\/span>, and <span class=\"st-mymarker-s\">how to create a empty list<\/span>.<\/p>\n\n\n\n<div class=\"wp-block-st-blocks-midashi-box freebox has-title\" style=\"background-color:#eceff1;border-color:#263238;border-radius:0 5px 5px 5px\"><p class=\"p-free\" style=\"border-color:#263238;font-weight:bold\"><span class=\"p-entry-f\" style=\"color:#ffffff;font-weight:bold;background-color:#263238;border-radius:0 0 5px 0\"><i class=\"st-fa st-svg-file-text-o st-css-no\" aria-hidden=\"\"><\/i>Contents<\/span><\/p><div class=\"free-inbox\">\n<p>\u25c6How to check if a list is empty<\/p>\n\n\n\n<ul><li>Using &#8220;if not statement&#8221;<\/li><li>Using &#8220;len( ) function&#8221;<\/li><li>Comparison with empty list<\/li><\/ul>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u25c6How to create a empty list<\/p>\n\n\n\n<ul><li>Using square brackets &#8220;[ ]&#8221;<\/li><li>Using &#8220;list( ) function&#8221;<\/li><li>[Supplement]How to create a empty 2D list<\/li><\/ul>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">How to check if a list is empty<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to check if a list is empty<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The following three ways are explained below.<\/p>\n\n\n\n<ul><li>Using &#8220;if not statement&#8221;<\/li><li>Using &#8220;len( ) function&#8221;<\/li><li>Comparison with empty list<\/li><\/ul>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Using &#8220;if not statement&#8221;<\/h3>\n\n\n\n<p>I show how to check if a list is empty by <span class=\"st-mymarker-s\">using &#8220;if not statement&#8221;<\/span>.<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Converting an empty list to the boolean type returns False.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>list_fruits = &#091;\"Apple\", \"Banana\", \"Orange\"]\r\nlist_empty = &#091;]\r\n\r\nprint(bool(list_fruits))\r\n# True\r\n\r\nprint(bool(list_empty))\r\n# False<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Use the above to check if a list is empty.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>list_fruits = &#091;\"Apple\", \"Banana\", \"Orange\"]\r\nlist_empty = &#091;]\r\n\r\nif not list_fruits :\r\n    print(\"list is empty\")\r\nelse :\r\n    print(\"list is not empty\")\r\n# list is not empty\r\n\r\nif not list_empty :\r\n    print(\"list is empty\")\r\nelse :\r\n    print(\"list is not empty\")\r\n# list is empty<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This way is recommended in &#8220;PEP 8 &#8212; Style Guide for Python Code&#8221;.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u30fbFor sequences, (strings, lists, tuples), use the fact that empty sequences are false:<\/p><p># Correct:<br>if not seq:<br>if seq:<\/p><p># Wrong:<br>if len(seq):<br>if not len(seq):<\/p><p><\/p><cite>Reference : <a rel=\"noreferrer noopener\" href=\"https:\/\/peps.python.org\/pep-0008\/#function-annotations\" target=\"_blank\">PEP 8 \u2013 Style Guide for Python Code<\/a><\/cite><\/blockquote>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Using &#8220;len( ) function&#8221;<\/h3>\n\n\n\n<p>I show how to check if a list is empty by <span class=\"st-mymarker-s\">using &#8220;len( ) function&#8221;<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>list_fruits = &#091;\"Apple\", \"Banana\", \"Orange\"]\r\nlist_empty = &#091;]\r\n\r\nif len(list_fruits) == 0:\r\n    print(\"list is empty\")\r\nelse :\r\n    print(\"list is not empty\")\r\n# list is not empty\r\n\r\nif len(list_empty) == 0:\r\n    print(\"list is empty\")\r\nelse :\r\n    print(\"list is not empty\")\r\n# list is empty<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Comparison with empty list<\/h3>\n\n\n\n<p>I show how to check if a list is empty by <span class=\"st-mymarker-s\">using &#8220;len( ) function&#8221;<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>list_fruits = &#091;\"Apple\", \"Banana\", \"Orange\"]\r\nlist_empty = &#091;]\r\n\r\nif list_fruits == &#091;]:\r\n    print(\"list is empty\")\r\nelse :\r\n    print(\"list is not empty\")\r\n# list is not empty\r\n\r\nif list_empty == &#091;]:\r\n    print(\"list is empty\")\r\nelse :\r\n    print(\"list is not empty\")\r\n# list is empty<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-display -->\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"6239864271\"\n     data-ad-format=\"auto\"\n     data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">How to create a empty list<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to create a empty list<\/span>.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Using square brackets &#8220;[ ]&#8221;<\/h3>\n\n\n\n<p>I show how to create a empty list by <span class=\"st-mymarker-s\">using square brackets &#8220;[ ]&#8221;<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>list_empty = &#091;]\r\n\r\nprint(list_empty)\r\n# &#091;]\r\n\r\nprint(len(list_empty))\r\n# 0<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Using &#8220;list( ) function&#8221;<\/h3>\n\n\n\n<p>I show how to create a empty list by using <span class=\"st-mymarker-s\">&#8220;list( ) function&#8221;<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>list_empty = list()\r\n\r\nprint(list_empty)\r\n# &#091;]\r\n\r\nprint(len(list_empty))\r\n# 0<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">[Supplement]How to create a empty 2D list<\/h3>\n\n\n\n<p>As supplement, I show <span class=\"st-mymarker-s\">how to create a empty 2D list.<\/span><\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In following example code, an empty list is created by two different ways.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>num = 5\r\nempty_list1 = &#091;&#091;]] * num\r\nempty_list2 = &#091;&#091;] for _ in range(num)]\r\n\r\nprint(empty_list1)\r\n# &#091;&#091;], &#091;], &#091;], &#091;], &#091;]]\r\nprint(empty_list2)\r\n# &#091;&#091;], &#091;], &#091;], &#091;], &#091;]]<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Both are empty lists, but behave differently.<\/p>\n\n\n\n<p>In following example code, an element is added to the empty list.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>empty_list1&#091;0].append(1)\r\nempty_list2&#091;0].append(1)\r\n\r\nprint(empty_list1)\r\n# &#091;&#091;1], &#091;1], &#091;1], &#091;1], &#091;1]]\r\nprint(empty_list2)\r\n# &#091;&#091;1], &#091;], &#091;], &#091;], &#091;]]<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This is due to the fact that the all empty lists generated by &#8221; [ [ ] ]* num &#8221; are objects with the same id.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>print(id(empty_list1&#091;0]))\r\n# 2135914429192\r\nprint(id(empty_list1&#091;1]))\r\n# 2135914429192\r\n\r\nprint(id(empty_list2&#091;0]))\r\n# 2135913306504\r\nprint(id(empty_list2&#091;1]))\r\n# 2135898942984<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>When creating an empty 2D list, it is preferable that each list be independent.<\/p>\n\n\n\n<p>Therefore, it is better to generate an empty 2D list by &#8221; [ [ ] for _ in range(num) ] &#8220;.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-matched-content-rows-num=\"4,2\"\n     data-matched-content-columns-num=\"1,4\"\n     data-matched-content-ui-type=\"image_stacked,image_stacked\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"2243394422\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>This article shows how to check if a list is empty, and how to create a empty list. How to check if  &#8230; <\/p>\n","protected":false},"author":1,"featured_media":3568,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,11],"tags":[16,19],"_links":{"self":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/3301"}],"collection":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/comments?post=3301"}],"version-history":[{"count":87,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/3301\/revisions"}],"predecessor-version":[{"id":4181,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/3301\/revisions\/4181"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media\/3568"}],"wp:attachment":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media?parent=3301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/categories?post=3301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/tags?post=3301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}