# Selecting terms on only one variable from a multiple-variable expression

Say I have a polynomial like

$x y^2+15x^2 y+x+3y+10$

and I want to obtain, say, only the coefficient in $x$ alone, namely a 1. Using

Coefficient[x y^2+15x^2 y+x+3y+10,x]

I obtain

1+y^2.

How can I just obtain the 1?

Thank you in advance.

• What about Coefficient[x y^2 + 15 x^2 y + x + 3 y + 10, x] /. y -> 0? May 13, 2016 at 16:08
• ... or {1, 0} /. CoefficientRules[x y^2 + 15 x^2 y + x + 3 y + 10]?
– kglr
May 13, 2016 at 16:14
• @BlacKow The actual problem I am facing is more complicated than this example, so I would prefer to make it as neat as possible. Anyway, I appreciate the tip.
– Alex
May 13, 2016 at 16:14
• @Alex Can you give an appropriate example that would show deficiency of my proposal? May 13, 2016 at 16:28
• It is essentially the number of different variables in the problem. At the moment I have around 15 and it may grow even larger. However, the answer you have given seems to work. Thank you very much.
– Alex
May 13, 2016 at 16:37

## 1 Answer

A little bit more general way:

coeff[p_, x_] := Coefficient[p, x] /. (# -> 0 & /@ Variables[p])
p = x y^2 + 15 x^2 y + x + 3 y + 10;
p2 = 3 t^2 + z;
p3 = 3 t^2 x + z;

coeff[p, x]
coeff[p2, t^2]
coeff[p3, t^2]
coeff[p3, x t^2]

(* 1 3 0 3 *)