# Extracting sublists with a specific signature from a multi-level list

The title is perhaps not clearly written as to my intent - so here is an example.

I have the following list:

{{{},{8,8}},{18,192},{{},{7,8}},{17,192},{{},{6,16}},{16,32},{{},{5,16}},{15,32},{{},{4,2}},{14,8},{{},{3,2}},{13,8},{{{{},{2,1}},{10,8}},{12,64}},{20,64},{{{{},{1,1}},{9,8}},{11,64}},{19,64}}


What I want is to form a nested list that just has the non-empty sublists of two elements.

i.e. from the example list I want to return

{{8, 8}, {18, 192}, {7, 8}, {17, 192}, {6, 16}, {16, 32}, {5,
16}, {15, 32}, {4, 2}, {14, 8}, {3, 2}, {13, 8}, {2, 1}, {10,
8}, {12, 64}, {20, 64}, {1, 1}, {9, 8}, {11, 64}, {19, 64}}


I have tried various combinations of Flatten and/or Join but I cannot seem to get what I would like. Something tells me this is probably not that difficult (though it obviously is for myself) - any help would be much appreciated.

• True - but it doesn't help with the variable levels I am left with. You must have edited your comment because you mentioned Catenate - which doesn't help here. Feb 24, 2022 at 12:13
• Cases[lst, List[_Integer, _Integer], Infinity] should do it.
– Alan
Feb 24, 2022 at 12:20
• @Alan - yep that works. Thanks. Feb 24, 2022 at 12:24
• This is a very good question and I proposed an edit of title on how to find empty sublist on multi levels or something of that sort. Now I see someone has changed it to extracting sublist with special signature?!? Would anyone new to MMA who want to learn this would search "Extracting sublists with a specific SIGNATURE" ?!!! Feb 25, 2022 at 16:13
• @MathX - Yeah I just noticed the edit someone did - I agree with you the title might be a tad obscure now. Feb 25, 2022 at 16:37

## 4 Answers

Similar to the approach taken by Syed, but in one pass:

yourList = {{{}, {8, 8}}, {18, 192}, {{}, {7, 8}}, {17,
192}, {{}, {6, 16}}, {16, 32}, {{}, {5, 16}}, {15,
32}, {{}, {4, 2}}, {14, 8}, {{}, {3, 2}}, {13,
8}, {{{{}, {2, 1}}, {10, 8}}, {12, 64}}, {20,
64}, {{{{}, {1, 1}}, {9, 8}}, {11, 64}}, {19, 64}};

Cases[yourList, {a_?NumericQ, b_?NumericQ}, All]

(* Out:
{ {8, 8}, {18, 192}, {7, 8}, {17, 192}, {6, 16}, {16, 32},
{5, 16}, {15, 32}, {4, 2}, {14, 8}, {3, 2}, {13, 8}, {2, 1},
{10, 8}, {12, 64}, {20, 64}, {1, 1}, {9, 8}, {11, 64}, {19, 64}}
*)

• I like that - I'll accept this as an "official" answer. Much appreciated. Feb 24, 2022 at 19:40

Empty List may be removed "by .. /. {}->Nothing" and nested lists by: "{x1_List, x2_List} :> Sequence[x1, x2]":

d = {{{}, {8, 8}}, {18, 192}, {{}, {7, 8}}, {17,
192}, {{}, {6, 16}}, {16, 32}, {{}, {5, 16}}, {15,
32}, {{}, {4, 2}}, {14, 8}, {{}, {3, 2}}, {13,
8}, {{{{}, {2, 1}}, {10, 8}}, {12, 64}}, {20,
64}, {{{{}, {1, 1}}, {9, 8}}, {11, 64}}, {19, 64}};
d //. {{} -> Nothing, {x1_List, x2_List} :> Sequence[x1, x2]}

(*{{8, 8}, {18, 192}, {7, 8}, {17, 192}, {6, 16}, {16, 32}, {5,
16}, {15, 32}, {4, 2}, {14, 8}, {3, 2}, {13, 8}, {2, 1}, {10,
8}, {12, 64}, {20, 64}, {1, 1}, {9, 8}, {11, 64}, {19, 64}} *)

• Yes, that works nicely. I appreciate that. Feb 24, 2022 at 12:25
alist = {{{}, {8, 8}}, {18, 192}, {{}, {7, 8}}, {17,
192}, {{}, {6, 16}}, {16, 32}, {{}, {5, 16}}, {15,
32}, {{}, {4, 2}}, {14, 8}, {{}, {3, 2}}, {13,
8}, {{{{}, {2, 1}}, {10, 8}}, {12, 64}}, {20,
64}, {{{{}, {1, 1}}, {9, 8}}, {11, 64}}, {19, 64}}


If you have to include complex and reals as well use the following variation (otherwise change to _Integer):

pos = Position[alist, {_?NumberQ, _?NumberQ}]

{{1, 2}, {2}, {3, 2}, {4}, {5, 2}, {6}, {7, 2}, {8}, {9,
2}, {10}, {11, 2}, {12}, {13, 1, 1, 2}, {13, 1, 2}, {13,
2}, {14}, {15, 1, 1, 2}, {15, 1, 2}, {15, 2}, {16}}

Extract[alist, pos]


Another way:

Notice the positions:

yourlist // Flatten // Partition[#,2]&