I would like to select elements from list by specifying their head. E.g.

SelectWithHead[{1,2,3.5,x}, Symbol] = {x}
SelectWithHead[{1,2,3.5,x}, Real] = {3.5}

What is the simpliest way for this?


Select[{1,2,3,5,x}, Head[#] === Symbol&] would work, but I would recommend using cases instead:

Cases[{1,2,3,5,x}, _Symbol]


One advantage of using the pattern _head is that it will not cause unwanted evaluation. To keep Head[#] === Symbol & from evaluating its argument would require rewriting it as something like Function[x, Head @ Unevaluated @ x == Symbol, HoldAll], whereas the Cases form allows this directly:

Cases[Hold[1 + 2, 3*4, 5^6], _Plus]

Although evaluation took place after the match _Plus successfully matched (only) the held expression of the form Plus[1, 2]. To return that expression also unevaluated we can use a delayed rule:

Cases[Hold[1 + 2, 3*4, 5^6], x_Plus :> Defer[x]]

{1 + 2}

(I chose Defer for this example but Hold or HoldForm may be used instead as required.)

| improve this answer | |
  • $\begingroup$ Ah, Cases, that wat was I looking for. Will accept ASAP $\endgroup$ – uranix May 9 '15 at 15:21
  • $\begingroup$ @Mr.Wizard thanks for the explanation! $\endgroup$ – uranix May 11 '15 at 13:22
  • $\begingroup$ @uranix You're welcome. $\endgroup$ – Mr.Wizard May 11 '15 at 23:05

Although most of the time I would prefer Cases there is an alternative worth mentioning: Pick.

expr = Hold[1 + 2, 3*4, 5^6];

Pick[expr, expr[[All, 0]], Plus]
Hold[1 + 2]

Part zero is used to extract the head and All represents a sequence therefore the original head Hold is retained. (See: Head and everything except Head?) Pick then finds the position(s) of literal Plus in Hold[Plus, Times, Power] and extracts the element at that position from the original Hold[1 + 2, 3*4, 5^6], again preserving the original head.

| improve this answer | |

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.