{"id":6183,"date":"2016-09-05T21:50:10","date_gmt":"2016-09-05T20:50:10","guid":{"rendered":"http:\/\/emilkirkegaard.dk\/en\/?p=6183"},"modified":"2016-09-05T21:50:10","modified_gmt":"2016-09-05T20:50:10","slug":"python-removing-empty-lists-from-mixed-nested-lists","status":"publish","type":"post","link":"https:\/\/emilkirkegaard.dk\/en\/2016\/09\/python-removing-empty-lists-from-mixed-nested-lists\/","title":{"rendered":"Python: Removing empty lists from mixed nested lists"},"content":{"rendered":"<p>Suppose you have some function that returns possibly nested lists with empty lists inside. Worse, they may have lists with lists with an empty list inside. And suppose you want to get rid of all the empty lists, including those that just have other lists with empty lists inside. Like this:<\/p>\n<pre>a = [1, [2, []], [], \"a\", [[]]]<\/pre>\n<p>I came up with the following pair of functions to get rid of these:<\/p>\n<pre>#meta function\r\ndef remove_empty_lists(l):\r\n    keep_going = True\r\n    prev_l = l\r\n    while keep_going:\r\n        #call remover on the list\r\n        new_l = remover(prev_l)\r\n        #are they identical objects?\r\n        if new_l == prev_l:\r\n            keep_going = False\r\n        #set prev to new\r\n        prev_l = new_l\r\n    #return the result\r\n    return new_l\r\n\r\n\r\n#function\r\ndef remover(l):\r\n    #new list\r\n    newlist = []\r\n    #loop over elements\r\n    for i in l:\r\n        #pdb.set_trace()\r\n        #is element a non-empty list? then call self on it\r\n        if isinstance(i, list) and len(i) != 0:\r\n            newlist.append(remover(i))\r\n        #if not a list\r\n        if not isinstance(i, list):\r\n            newlist.append(i)\r\n    \r\n    #return newlist\r\n    return newlist\r\n<\/pre>\n<p>Testing:<\/p>\n<pre>&gt;&gt;&gt; remover(a)\r\n[1, [2], 'a', []]\r\n&gt;&gt;&gt; remove_empty_lists(a)\r\n[1, [2], 'a']<\/pre>\n<p>The first only runs thru the elements once and thus neglects to rid rid of the second-order empty list at the end. The second function keeps removing empty lists until the function no longer has an effect and the process comes to a halt.<\/p>\n<p>I have no idea no fast this is compared to other methods, but it gets the job done!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Suppose you have some function that returns possibly nested lists with empty lists inside. Worse, they may have lists with lists with an empty list inside. And suppose you want to get rid of all the empty lists, including those that just have other lists with empty lists inside. Like this: a = [1, [2, [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2383],"tags":[],"class_list":["post-6183","post","type-post","status-publish","format-standard","hentry","category-python","entry"],"_links":{"self":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts\/6183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/comments?post=6183"}],"version-history":[{"count":1,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts\/6183\/revisions"}],"predecessor-version":[{"id":6184,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts\/6183\/revisions\/6184"}],"wp:attachment":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/media?parent=6183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/categories?post=6183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/tags?post=6183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}