# How to convert a polynomial into a list?

I have a list of polynomials. Each polynomial looks like:

p[1, 2, 5] p[3, 6, 9] p[4, 7, 8] - 3 p[1, 2, 4] p[3, 6, 9] p[5, 7, 8]


How to convert each of these polynomials into a list. For example, I would like the above become:

{{1, {1, 2, 5}, {3, 6, 9}, {4, 7, 8}}, {-3, {1, 2, 4}, {3, 6, 9}, {5, 7, 8}}}


Thank you very much!

• Lists must use list brackets. See The Four Kinds of Bracketing in the Wolfram Language Commented Mar 30 at 2:07
• Apply[List, expr, {0, 2}]
– I.M.
Commented Mar 30 at 8:45
• ...or even Apply[List, X, {0, ∞}] to be more general Commented Mar 30 at 10:08
• @I.M. This doesn't give the coefficient 1 for the first term though Commented Mar 30 at 22:38
• @MelaGo, right, perhaps having 1 might be redundant like mult by 1, up to OP thou
– I.M.
Commented Mar 31 at 9:23

A variant of E. Chan-López answer

pol = p[1, 2, 5] p[3, 6, 9] p[4, 7, 8] - 3 p[1, 2, 4] p[3, 6, 9] p[5, 7, 8];

ReplaceAll[p :> List] @ Query[All, All, 1][FactorList /@ List @@ pol]


{{1, {1, 2, 5}, {3, 6, 9}, {4, 7, 8}}, {-3, {1, 2, 4}, {3, 6, 9}, {5, 7, 8}}}

Or, as Roman commented:

(FactorList /@ List @@ pol)[[All, All, 1]] /. p -> List


same result

• You can write this simpler as (FactorList /@ List @@ X)[[All, All, 1]] /. p -> List Commented May 3 at 16:43
• Thank you, Roman, I updated the answer
– eldo
Commented May 3 at 16:53
pol = p[1, 2, 5]  p[3, 6, 9]  p[4, 7, 8] - 3  p[1, 2, 4]  p[3, 6, 9]  p[5, 7, 8];


Using FactorList and the following rules:

FactorList[#][[All, 1]] & /@ (pol /. Plus[a_, b_] :> {a, b}) /. p[a___] :> {a}


{{1, {1, 2, 5}, {3, 6, 9}, {4, 7, 8}}, {-3, {1, 2, 4}, {3, 6, 9}, {5, 7, 8}}}

pol = p[1, 2, 5] p[3, 6, 9] p[4, 7, 8] - 3 p[1, 2, 4] p[3, 6, 9] p[5, 7, 8];

If[AtomQ[#[[1]]], List @@@ #, Join[{1}, List @@@ #]] & /@
List @@@ List @@ pol


{{1, {1, 2, 5}, {3, 6, 9}, {4, 7, 8}}, {-3, {1, 2, 4}, {3, 6, 9}, {5, 7, 8}}}

polynomial =
p[1, 2, 5] p[3, 6, 9] p[4, 7, 8] -
3 p[1, 2, 4] p[3, 6, 9] p[5, 7, 8];

(*Convert the polynomial into a list of terms*)
termsList = List @@ polynomial;

(*Map each term to extract the coefficient and the indices*)
formattedList = termsList /. Plus | Minus -> Sequence;
formattedList =
formattedList /.
a_. p[x_, y_, z_] p[u_, v_, w_] p[s_, t_, q_] :> {a, {x, y, z}, {u,
v, w}, {s, t, q}};

formattedList

{{1, {4, 7, 8}, {3, 6, 9}, {1, 2, 5}}, {-3, {5, 7, 8}, {3, 6, 9}, {1, 2, 4}}}