Deleting certain elements from a nested list
For the record, so that this question have answers in an Answer block, correct answers given in the comments to the OP include
list/.{_, 0} :> Sequence[]
DeleteCases[list, {_, 0}, ∞]
In addition, here are two that are similar to the ones above but slightly more specific to the requirements stated in the OP:
list/.{_, 0} -> Sequence[]
(Rule
instead ofRuleDelayed
)DeleteCases[list, {_, 0}, {3}]
(deletes only sublists at level 3)
Let's use a more complex expression than given above to stress the solution methods a bit more.
data =
{{{{a, 0}, {b, 1}, {c, 2}}, {{d, 4}, {e, 5}, {f, 6}}},
{{{{{a, 0}}}, {b, 1}, {c, 2}}, {{d, 4}, {{e, 0}, {f, 6}}}}};
I think the following have advantages over the solutions already given.
data /. {_, 0} -> Nothing
DeleteCases[data, {_, 0}, {-2}]
Both give
{{{{b, 1}, {c, 2}}, {{d, 4}, {e, 5}, {f, 6}}}, {{{{}}, {b, 1}, {c, 2}}, {{d, 4}, {{f, 6}}}}}
The first method I think more easily understood than /. {_, 0} :> Sequence[]
and second simply is more robust -- DeleteCases[data, {_, 0}, {3}]
fails on this more complex data list.