{"id":112345,"date":"2020-05-02T07:58:41","date_gmt":"2020-05-02T07:58:41","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=8094"},"modified":"2020-05-02T07:58:41","modified_gmt":"2020-05-02T07:58:41","slug":"how-to-remove-empty-lists-from-a-list-of-lists-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/05\/02\/how-to-remove-empty-lists-from-a-list-of-lists-in-python\/","title":{"rendered":"How to Remove Empty Lists from a List of Lists in Python?"},"content":{"rendered":"<p><strong>Short answer: You can remove all empty lists from a list of lists by using the list comprehension statement <code>[x for x in list if x]<\/code> to filter the list. <\/strong><\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"How to Remove Empty Lists from a List of Lists in Python?\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/Fi3zvSaVY8U?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p>In the following, you&#8217;ll learn about the two methods using list comprehension and the filter() function to remove all empty lists from a list of lists. <\/p>\n<p>But before that, feel free to play with the code yourself:<\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/659a4282bd\" width=\"100%\" height=\"356\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe> <\/p>\n<h2>Method 1: List Comprehension<\/h2>\n<p>How can you remove all empty lists from a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list-of-lists\/\" target=\"_blank\">list of lists<\/a>? Say, you&#8217;ve got a list of lists<\/p>\n<p><code>[[1, 2, 3], [1, 2], [], [], [], [1, 2, 3, 4], [], []]<\/code> <\/p>\n<p>and you want all empty lists removed to obtain the list of lists <\/p>\n<p><code>[[1, 2, 3], [1, 2], [1, 2, 3, 4]]<\/code>. <\/p>\n<p><strong>Solution<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\">list comprehension<\/a> <code>[x for x in list if x]<\/code> to filter the list and remove all lists that are 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 = [[1, 2, 3], [1, 2], [], [], [], [1, 2, 3, 4], [], []]\nprint([x for x in lst if x])\n# [[1, 2, 3], [1, 2], [1, 2, 3, 4]]<\/pre>\n<p>The condition if <code>x<\/code> evaluates to <code>False<\/code> only if the list <code>x<\/code> is empty. In all other cases, it evaluates to <code>True<\/code> and the element is included in the new list.<\/p>\n<p>You can visualize the execution flow here by clicking the &#8220;Next&#8221; button:<\/p>\n<p> <iframe loading=\"lazy\" width=\"800\" height=\"500\" frameborder=\"0\" src=\"https:\/\/pythontutor.com\/iframe-embed.html#code=lst%20%3D%20%5B%5B1,%202,%203%5D,%20%5B1,%202%5D,%20%5B%5D,%20%5B%5D,%20%5B%5D,%20%5B1,%202,%203,%204%5D,%20%5B%5D,%20%5B%5D%5D%0Aprint%28%5Bx%20for%20x%20in%20lst%20if%20x%5D%29&#038;codeDivHeight=400&#038;codeDivWidth=350&#038;cumulative=false&#038;curInstr=0&#038;heapPrimitives=nevernest&#038;origin=opt-frontend.js&#038;py=3&#038;rawInputLstJSON=%5B%5D&#038;textReferences=false\"> <\/iframe> <\/p>\n<h2>Method 2: filter()<\/h2>\n<p>An alternative is to use the <code>filter()<\/code> function to remove all empty lists from a list of lists:<\/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=\"\">\nlst = [[1, 2, 3], [1, 2], [], [], [], [1, 2, 3, 4], [], []]\nprint(list(filter(lambda x: x, lst)))\n<\/pre>\n<p>The <a href=\"https:\/\/blog.finxter.com\/how-to-filter-a-list-in-python\/\">filter()<\/a> function takes two arguments: <\/p>\n<ul>\n<li>the <strong>filter decision function <\/strong>to check for each element whether it should be included in the filtered iterable (it returns a Boolean value), and <\/li>\n<li>the <strong>iterable <\/strong>to be filtered.<\/li>\n<\/ul>\n<p>As filter decision function, you use the identity function that just passes the list through. Why does this work? Because only an empty list will be evaluated to <code>False<\/code>. All other lists will be evaluated to <code>True<\/code> (and, thus, pass the filtering test). <\/p>\n<p><strong>Related articles:<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/blog.finxter.com\/how-to-filter-a-list-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to filter a list in Python?<\/a><\/li>\n<li><a href=\"https:\/\/blog.finxter.com\/python-lists-filter-vs-list-comprehension-which-is-faster\/\">Filter vs List Comprehension (Speed)<\/a><\/li>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\">List Comprehension<\/a><\/li>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list-of-lists\/\" target=\"_blank\">Lists of Lists<\/a><\/li>\n<\/ul>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?<\/p>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: You can remove all empty lists from a list of lists by using the list comprehension statement [x for x in list if x] to filter the list. In the following, you&#8217;ll learn about the two methods using list comprehension and the filter() function to remove all empty lists from a list of [&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-112345","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\/112345","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=112345"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/112345\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=112345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=112345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=112345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}