Message Boards Message Boards

How to select only empty elements into a new list?

Posted 10 years ago

How can I take a list and remove all empty elements...

 {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, 
{}, {}, {}, {}, {}, {}, {}, {}, {"nonempty element"}, {}, {}, {}, {}, {}}
POSTED BY: Wael Elrifai
4 Replies
Posted 10 years ago

Thanks Isaac, I'm really just starting out with Wolfram so I'm making basic errors. Problem solved!

POSTED BY: Wael Elrifai

The above code was Mma7. I tried the same in (the free) Wolfram|Alpha, and the (free) programming cloud which also returned,

Select[ {{}, {}, {2}, {}}, # != {}&]
{2}

Would you try re-starting your kernel, clear memory with `Remove["Global'*"] (That is a "Global backtick Star" as the first line in your file) and then try again? I can't think of any other reasons why this would fail.

POSTED BY: Isaac Abraham
Posted 10 years ago

Thanks Isaac, but I can't seem to quite get this right...

In: sparse_list= {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {"nonempty element"}, {}}; Select[sparse_list, # != {} &]

Out: Pattern[]

Any thoughts?

POSTED BY: Wael Elrifai

This may be what you seek.

Select[list, # != {} &]
(* Out:= {{"nonempty element"}} *)
POSTED BY: Isaac Abraham
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract