Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to remove empty lists from lists?

What is the most efficient way to remove all empty List[] objects from all of the Lists that appear in an expression at different levels? The empty List[] should be removed only if it is an element of another List itself.

like image 625
Alexey Popkov Avatar asked Jul 03 '11 12:07

Alexey Popkov


People also ask

How do you filter an empty list in Python?

An alternative is to use the filter() function to remove all empty lists from a list of lists. The filter() function takes two arguments: The filter decision function to check for each element whether it should be included in the filtered iterable (it returns a Boolean value), and. The iterable to be filtered.

How do I remove an empty string from a nested list in Python?

How do I remove an empty string from a nested list in Python? Use filter() to remove empty strings from a list. Call filter(function, iterable) with a lambda function that checks for the empty string as function and a list as iterable . Use list() to convert the resultant filter object to a list.


1 Answers

Andrew and Alexey point out that using expr //. x_List :> DeleteCases[x, {}, Infinity] as I had in my previous answer will also remove the {} in blah[{f[{}]}], whereas it should leave it untouched as its head is f, not a List. The solution, thanks to Leonid, is to not use ReplaceRepeated, but Replace instead with replacements being made at all levels from 0 through Infinity:

Replace[expr, x_List :> DeleteCases[x, {}], {0, Infinity}]

The reason why Replace works and ReplaceRepeated doesn't can be seen from this little example. Consider expr = {a, {}, {b, {}}, c[d, {}]}; in its TreeForm

enter image description here

Replace works by starting with the innermost expression(s) first, i.e., List[b,{}] and c[d,{}], and works upwards to the top node. At each level, checking the head is as simple as looking up to the node right above and see if it matches List. If it does, apply the rule and move up a level, else do nothing and move up a level. This results in a final tree:

enter image description here

ReplaceRepeated (//.), on the other hand, works by starting with the top most node and traversing down the tree. The previous solution starts by checking if the first node is a List and if it is, then DeleteCases is applied and it moves down the tree, ruthlessly replacing every {} it can find. Note that it does not check if the heads of the inner expressions also match List, because this traversal is done by DeleteCases, not ReplaceRepeated. When //. moves to subsequent lower nodes, there is nothing more to replace and it exits quickly. This is the tree that one gets with the previous solution:

enter image description here

Note that the {} inside c[d, {}] has also been removed. This is solely due to the fact that DeleteCases (with level specification {0,Infinity} moves down the tree. Indeed, if the first head had been something other than List, it would've skipped it and moved to the next level, of which only the {} in {b, {}} is a match. To demostrate with expr2 = f[a, {}, {b, {}}, c[d, {}]], we get

enter image description here

Note that in the current solution with Replace, we use DeleteCases with the default level specification, which is the first level only. It does not, therefore, check for and delete empty lists deeper than on the first level, which is exactly what we need here.

Although we used the first node to explain why it fails, the reasoning holds true for every node. Leonid explains these concepts in greater detail in his book

like image 147
abcd Avatar answered Oct 13 '22 19:10

abcd