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).

  • $\begingroup$ 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, {}}}? $\endgroup$ – m_goldberg Oct 25 '13 at 23:26
  • 2
    $\begingroup$ This question has been asked before. Follow the link for what seems to be the optimal and general solution. $\endgroup$ – 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}}
  • $\begingroup$ ,Why Select[list, Length@# != 0 &] cannot work?Can you tell me why?Thanks. $\endgroup$ – xyz Oct 26 '13 at 4:27
  • $\begingroup$ @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) $\endgroup$ – 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[## &[]]
  • $\begingroup$ This should come in handy from time to time. $\endgroup$ – DavidC Oct 25 '13 at 22:39
  • $\begingroup$ Is sequence only used for empty sets or can I use them in other cases? $\endgroup$ – thaibak527 Oct 25 '13 at 22:46
  • $\begingroup$ @thaibak527 Try this {{a, b}, {a, b, c}, {a, b, c, d}} /. r : {x_, y_} :> Sequence @@ r $\endgroup$ – Dr. belisarius Oct 25 '13 at 22:49
  • $\begingroup$ @Dr.belisarius, expr //. {} -> Nothing also works in the first example. $\endgroup$ – alancalvitti Nov 11 '16 at 15:19

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}}

  • $\begingroup$ 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. $\endgroup$ – Alexey Popkov Oct 26 '13 at 13:51
  • $\begingroup$ @AlexeyPopkov Thanks for pointing that out, I was thinking "too fast" (or simply too little) so I didn't realize... $\endgroup$ – C. E. Oct 26 '13 at 14:03

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).

{{"Andy", {}, {}, {}, {"Jeff", "Tim"}, {}, {"Zach"}}} /. {} -> Nothing

Replace empty sets with Nothing gives

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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