# Split list when there is an empty row

I am trying to split a list when there is an empty row. I have tried the following:

a = {{1, 1, 1}, {2, 2, 2}, {, ,}, {3, 3, 3}};
b = Split[a, # == Null]


but my result is:

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


which is not correctly split.

I would like to have {1,..} and {2,...} gathered together:

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

• A pure function needs to end with &: b = Split[a, # == Null &] Commented Aug 2, 2016 at 14:40
• Related (6727) Commented Aug 2, 2016 at 15:03

Read carefully documentation of Split, you need its friend SplitBy:

SplitBy[a, MemberQ[Null]]


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

Here's one way:

Split[{{1, 1, 1}, {2, 2, 2}, {, ,}, {3, 3, 3}},
! (MatchQ[#1, {Null ..}] || MatchQ[#2, {Null ..}]) &]

• Awesome !! That's what I was looking for! Thank you! Commented Aug 2, 2016 at 14:41

Here two ways to do it using Split.

a = {{1, 1, 1}, {2, 2, 2}, {, ,}, {3, 3, 3}, {4, 4, 4}};

With[{p1 = {{_Integer ..} ..}}, Split[a, MatchQ[{#1, #2}, p1] &]
With[{p2 = {{Except[Null] ..} ..}}, Split[a, MatchQ[{#1, #2}, p2] &]]]


Both give

{{{1, 1, 1}, {2, 2, 2}}, {{Null, Null, Null}}, {{3, 3, 3}, {4, 4, 4}}}

The form using pattern p2 is probably better because it is more general. It will handle

c = {{1, 1, 1}, {2, 2, 2}, { , ,}, {x, y, z}, {4, 4, 4}};


correctly, while the form using p1 stumbles on the sub-list {x, y, z}.

With[{p1 = {{Except[Null] ..} ..}}, Split[c, MatchQ[{#1, #2}, p1] &]]


{{{1, 1, 1}, {2, 2, 2}}, {{Null, Null, Null}}, {{x, y, z}, {4, 4, 4}}}