12
$\begingroup$

I have a list:

list={{{1},{2},{1,2},{1,2,3},{},{}},{{2},{3},{}}}

I would like to remove the empty lists {} to get:

{{{1},{2},{1,2},{1,2,3}},{{2},{3}}}

How can I automate this using Mathematica ?

$\endgroup$
2

5 Answers 5

22
$\begingroup$

Using ReplaceAll (which can also be written /.) we can replace all the empty lists { } with Nothing.

list /. {} -> Nothing

{{{1}, {2}, {1, 2}, {1, 2, 3}}, {{2}, {3}}}
$\endgroup$
10
$\begingroup$

Using DeleteCases:

list1 = {{{1}, {2}, {1, 2}, {1, 2, 3}, {}, {}}, {{2}, {3}, {}}};

DeleteCases[list1, {}, 2]

(*{{{1}, {2}, {1, 2}, {1, 2, 3}}, {{2}, {3}}}*)

Considering the @Nasser's observation, we can generalize this strategy introducing the depth of the array to apply it in more complicated cases, as I show below:

DeleteCases[#, {}, Depth@#] &@list1

(*{{{1}, {2}, {1, 2}, {1, 2, 3}}, {{2}, {3}}}*)

list2 = {{{1}, {2}, {1, 2}, {1, 2, 3}, {}, {}}, {3, {{{2}, {3}, {}}}}};

DeleteCases[#, {}, Depth@#] &@list2

(*{{{1}, {2}, {1, 2}, {1, 2, 3}}, {3, {{{2}, {3}}}}}*)

Thanks to Nasser for this valuable observation.

Another way, as suggested by @AndreasLauschke is to put Infinity instead of 2:

DeleteCases[list1, {}, ∞]

(*{{{1}, {2}, {1, 2}, {1, 2, 3}}, {{2}, {3}}}*)
$\endgroup$
3
  • 2
    $\begingroup$ fyi, this does not work on general. It works for the input given. For example, if the input was list={{{1},{2},{1,2},{1,2,3},{},{}},{3,{{{2},{3},{}}}}}; then DeleteCases[list,{},2] does not remove the {}. Screen shot !Mathematica graphics $\endgroup$
    – Nasser
    Commented Dec 26, 2023 at 23:37
  • 2
    $\begingroup$ Use Infinity instead of 2 $\endgroup$ Commented Dec 26, 2023 at 23:46
  • $\begingroup$ @AndreasLauschke You're right, thanks for the suggestion. :-) $\endgroup$ Commented Dec 26, 2023 at 23:49
7
$\begingroup$
list = {{{1}, {2}, {1, 2}, {1, 2, 3}, {}, {}}, {{2}, {3}, {}}};

p = Position[{}] @ list;

{{1, 5}, {1, 6}, {2, 3}}

Delete[p] @ list

{{{1}, {2}, {1, 2}, {1, 2, 3}}, {{2}, {3}}}

MapAt[Nothing, p] @ list

{{{1}, {2}, {1, 2}, {1, 2, 3}}, {{2}, {3}}}

ReplacePart[p -> Nothing] @ list

{{{1}, {2}, {1, 2}, {1, 2, 3}}, {{2}, {3}}}

ReplaceAt[_ :> Nothing, p] @ list

{{{1}, {2}, {1, 2}, {1, 2, 3}}, {{2}, {3}}}

$\endgroup$
5
$\begingroup$
list //. {} -> Sequence[]

and

list //. {} :> Unevaluated[## &[]]

both give

{{{1}, {2}, {1, 2}, {1, 2, 3}}, {{2}, {3}}}

$\endgroup$
2
  • 1
    $\begingroup$ (+1) I hadn't seen the second one, nicely done mate! :-) $\endgroup$ Commented Dec 27, 2023 at 4:04
  • 1
    $\begingroup$ @E.Chan-López thanks a lot! :-) $\endgroup$
    – bmf
    Commented Dec 27, 2023 at 5:09
1
$\begingroup$
list = {{{1}, {2}, {1, 2}, {1, 2, 3}, {}, {}}, {{2}, {3}, {}}}

Using SequenceCases

SequenceCases[#, {a_, {} ...} :> a] & /@ list

{{{1}, {2}, {1, 2}, {1, 2, 3}}, {{2}, {3}}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.