# Python Sympy – Select monomials from the ANF of a Boolean expression

## Question:

I have some truth table in GF(2) and I compute the corresponding Algebraic Normal Form (ANF) or Zhegalkin polynomial. Below a dummy example with two variables.

``````from sympy.logic.boolalg import ANFform
from sympy.abc import x, y

truth_table = [0, 1, 1, 1]

expr = ANFform([x, y], truth_table)
``````

which prints

``````x ^ y ^ (x & y)
``````

I would then like to extract and select the individuals monomials of the resulting expression.

For example, I would have something like this:

``````expr_monoms[2] # (x & y)
``````

How to achieve this?

You can use `.args`:

``````In [50]: from sympy.logic.boolalg import ANFform
...: from sympy.abc import x, y
...:
...: truth_table = [0, 1, 1, 1]
...:
...: expr = ANFform([x, y], truth_table)

In [51]: expr
Out[51]: x ⊻ y ⊻ (x ∧ y)

In [52]: expr.args[2]
Out[52]: x ∧ y
``````
Categories: questions
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.