Assume I have:

list = {{Mean[{}], 668}, {1, 2}, {Mean[{}], 2}, {7, 8}}

and I want to select only pairs, where the first element is not equal to Mean[{}].

Then I find two solutions, which I not really understand:

Select[list, #[[1]] > 0 &]

{{1, 2}, {7, 8}}

Select[list, Length[#[[1]]] == 0 &]

{{1, 2}, {7, 8}}

Can you explain me why these examples produce the correct result?

Which solution would you propose?

  • 2
    $\begingroup$ The first element of your pairs is either a positive integer, obviously with length 0, or a non atomic expression, with positive length. Therefore both solutions work. An alternative is Cases[list, {_Integer, _}]. $\endgroup$ – Fred Simons Mar 14 at 12:12
  • $\begingroup$ @Fred Simons: I like your solution more ... $\endgroup$ – lio Mar 14 at 12:14
  • 1
    $\begingroup$ also DeleteCases[list, {_Mean, _}] $\endgroup$ – kglr Mar 14 at 12:32

If you run


you will find that this does not return True. When you run the Select command it returns those elements for which the condition is True. While Mean[{}]>0 doesn't return False, nor does it return True.

For the second one, Length[Mean[{}]] evaluates to 1, and so Length[Mean[{}]]==0 is false. If you want to do this more explicitly you can write:

Select[list, #[[1]] =!= Mean[{}] &]
  • $\begingroup$ What is the meaning of =!=? $\endgroup$ – lio Mar 14 at 12:16
  • $\begingroup$ It returns true unless the two expressions are identical. It is the negation of === $\endgroup$ – Jonathan Shock Mar 14 at 12:19
  • 3
    $\begingroup$ @lio, a tip for the future: everytime you encounter an unfamiliar symbol in Mathematica, highlight it in a notebook, and press F1. $\endgroup$ – J. M. will be back soon Mar 14 at 12:23

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.