Suppose I have a list of the form
list={a+b,c+d,Xi+Psi*Zeta,0}
How can I make a list of only the first term of each element in that list? In this case the list would be just
list2={a,c,Xi,0}
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityIn this case the list would be just list2={a,c,Xi,0}
For this example you show, this should it do it. But I do not know if this will fail or not for other examples. If you can provide more examples, it will help test it. These parsing things can be tricky depending on input
list = {a + b, c + d, Psi*Zeta + Xi, 0};
If[AtomQ[#], #, First[#]] & /@ list
Another example to handle more general cases
list = {a + b, c + d, Psi*Zeta + Xi, 0, Exp[x], Sin[x],
x + Sin[x], 1 + Exp[x], x*Sin[x], Sin[x]^2, 1/x + 2,
{1, 2, 3}};
If[AtomQ[#] || Length[#] == 1 || Head[#] === Power ||
Head[#] === List, #, First[#]] & /@ list
ps. This might still fail on some other input. More example will help.
This looks like a case for the (often-overlooked) two-argument form of First
:
list = {a + b, c + d, Psi*Zeta + Xi, 0};
First[#, #] & /@ list
{a, c, Xi, 0}
Second example from Nasser's answer with exceptions can be handled using:
listb = {a + b, c + d, Psi*Zeta + Xi, 0, Exp[x], Sin[x], x + Sin[x],
1 + Exp[x], x*Sin[x], Sin[x]^2, 1/x + 2, {1, 2, 3}};
listb /. $pat : Except[_Power | _List | _?(Length @ # == 1 &)] :> First[$pat, $pat]
c + a
is automatically converted toa + c
? $\endgroup$Xi
the "first" term inPsi*Zeta+Xi
and notPsi*Zeta
$\endgroup$