# Select elements from a list depending on condition

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, #[] > 0 &]

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

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

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


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

Which solution would you propose?

• 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, _}]. – Fred Simons Mar 14 at 12:12
• @Fred Simons: I like your solution more ... – lio Mar 14 at 12:14
• also DeleteCases[list, {_Mean, _}] – kglr Mar 14 at 12:32

If you run

Mean[{}]>0


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, #[] =!= Mean[{}] &]

• What is the meaning of =!=? – lio Mar 14 at 12:16
• It returns true unless the two expressions are identical. It is the negation of === – Jonathan Shock Mar 14 at 12:19
• @lio, a tip for the future: everytime you encounter an unfamiliar symbol in Mathematica, highlight it in a notebook, and press F1. – J. M. will be back soon Mar 14 at 12:23