# Find elements of a list which are empty

Sorry for the most basic of questions. How would I find the positions of a list of elements which are empty, for example I have the list

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


and I want to find the indices of the empty elements.

-
Position[{1, 2, {}, {}, 3, 5}, {}] yields {{3}, {4}} –  Artes Jun 13 '13 at 11:02
Additionally you can get all elements of the list that are not {} using Cases[{1, 2, {}, {}, 3, 5}, Except[{}]]. –  mmal Jun 13 '13 at 11:25
This is better DeleteCases[{1, 2, {}, {}, 3, 5}, {}]. –  Artes Jun 13 '13 at 12:55
@Artes I would say its equivalent. –  mmal Jun 13 '13 at 15:05
@mmal It is not. –  Artes Jun 13 '13 at 15:47

If you want to find the position of something in a given expression, you should use Position.

In this example, you could simply write:

Position[{1, 2, {}, {}, 3, 5}, {}]
(*{{3}, {4}}*)


I have to mention that this not the whole power of Position: the second parameter of Position can be a "pattern"(which may be not known by a matlab coder:)), look up the doc for more imformation.

-
Great thanks, I know a little about regexp –  mor22 Jun 13 '13 at 11:28
@mor22 a Pattern in Mathematica is different from a regular expression. They're similar in some respects, but a pattern is more broad. –  rcollyer Jun 13 '13 at 12:11
@rcollyer Thanks, I'm still learning! –  mor22 Jun 14 '13 at 7:22