Use Cases with empty cells in the list
For this use case, I would encourage you to use Map
and Replace
rather than Cases
, since you can use multiple rules for the extraction:
Map[Replace[{{___, {}} :> Null, {___, {x_}} :> x}],
dat1]
(* {Null, 1, 2} *)
As an alternative, you can use Replace
with a level spec:
Replace[dat1, {{___, {}} :> Null, {___, {x_}} :> x}, 1]
(* {Null, 1, 2} *)
Here I treat the contents of the empty element as Null
and I assume either 1 element in the nested list or no element:
Cases[dat1, {x_ : Null} :> x, 2]
(* returns: {Null, 1, 2} *)