# Removing empty sets from lists

If I had a list, say,

names= {"Andy", "Bob", "Carly", "Sandy", "Jeff", "Tim", "Tom", "Zach"}


and I decide to pick a characteristic only a certain of them have, and my input would be something that works, (or think of it as something WordData["....",",,,,"], and my output would look like:

{{"Andy", {}, {}, {}, {"Jeff", "Tim"}, {}, {"Zach"}}


I know this type of thing probably isn't possible, but just say if it did, how would I only choose the nonempty set?

How would I do this using Select? I know I should select from my set of names, but not sure (There's probably a different way, but I wanted to continue to practice this function so I can understand it fully).

-
Your example list {{Andy, {}, {}, {}, {Jeff, {{}}, Tim}, {}, {Zach, {}}} is not a valid Mathematica list. Did you mean {{Andy}, {}, {}, {}, {Jeff, {{}}, Tim}, {}, {Zach, {}}} or {Andy, {}, {}, {}, {Jeff, {{}}, Tim}, {}, {Zach, {}}}? – m_goldberg Oct 25 '13 at 23:26
This question has been asked before. Follow the link for what seems to be the optimal and general solution. – Leonid Shifrin Oct 25 '13 at 23:41

Assuming "choose the nonempty sets" means to remove Nulls, this can be done with Select

list={Andy, {}, {}, {}, {Jeff, Tim}, {}, {Zach}};
Select[list, UnsameQ[#, {}] &]

{Andy, {Jeff, Tim}, {Zach}}

-
,Why Select[list, Length@# != 0 &] cannot work?Can you tell me why?Thanks. – Shutao Tang Oct 26 '13 at 4:27
@Tangshutao - This removes the first entry Andy. The reason this is removed is because the length of the symbol Andy is zero, and so it does not fulfill the criterion (that Length !=0) – bill s Oct 26 '13 at 4:34

This works for any level (recursively):

{Andy, {}, {}, {}, {Jeff, {{}}, Tim}, {}, {Zach, {}}} //. {} -> Sequence[]
(*
{Andy, {Jeff, Tim}, {Zach}}
*)


Or the cryptic

{Andy, {}, {}, {}, {Jeff, {{}}, Tim}, {}, {Zach, {}}} //. {} :> Unevaluated[## &[]]

-
This should come in handy from time to time. – DavidC Oct 25 '13 at 22:39
Is sequence only used for empty sets or can I use them in other cases? – thaibak527 Oct 25 '13 at 22:46
@thaibak527 Try this {{a, b}, {a, b, c}, {a, b, c, d}} /. r : {x_, y_} :> Sequence @@ r – Dr. belisarius Oct 25 '13 at 22:49

As Leonid says, perhaps the most efficient and general solution to this problem is to use Replace and DeleteCases and I'll document it here as well:

Replace[expr, x_List :> DeleteCases[x, {}], {0, Infinity}]
(* {{Andy, {Jeff, Tim}, {Zach}}} *)


Read the linked post and the comments for an idea of how this works and the different edge cases that need to be considered (perhaps also look at the revisions to understand the different road bumps with each version).

-

This is also a prime candidate for DeleteCases, here's the equivalent of belisarius method for empty lists nested just one level down:

DeleteCases[{Andy, {}, {}, {}, {Jeff, {{}}, Tim}, {}, {Zach, {}}}, {{} ...}, Infinity]


{Andy, {Jeff, Tim}, {Zach}}

-
Strictly speaking, your solution is not equivalent to belisarius' method, because deeply nested empty lists will not be removed: DeleteCases[{{Zach, {{{{}}}}}}, {{} ...}, Infinity]. This topic is discussed here in details. – Alexey Popkov Oct 26 '13 at 13:51
@AlexeyPopkov Thanks for pointing that out, I was thinking "too fast" (or simply too little) so I didn't realize... – C. E. Oct 26 '13 at 14:03