TExcelTAD
Home › Algebra › Combining Like Terms

Combining Like Terms

Combining like terms is the basic tidying step of algebra: terms that have exactly the same variable part are added by adding their coefficients, so 4x+7x4x + 7x becomes 11x11x. This lesson shows how to spot like terms, how to combine them with signs, and how the same idea collects counts in code.

What "like" means

Two terms are like terms when their variable parts are identical: the same letters, each raised to the same power. Only the coefficients may differ.

Like terms Not like terms Why not
3x3x and −8x-8x 3x3x and 3x23x^2 different powers
5ab5ab and 2ba2ba 5ab5ab and 5a5a different letters
x2yx^2y and −4x2y-4x^2y x2yx^2y and xy2xy^2 powers on different letters
7 and −2 (constants) 7 and 7x7x one has no variable

Tip: Think of variables as units. 3 apples+5 apples=8 apples3\ \text{apples} + 5\ \text{apples} = 8\ \text{apples}, but 3 apples+5 pears3\ \text{apples} + 5\ \text{pears} cannot become one number. 3x+5x=8x3x + 5x = 8x, but 3x+5y3x + 5y stays as it is.

How to combine

Combining like terms is the distributive property read backwards: 4x+7x=(4+7)x=11x4x + 7x = (4 + 7)x = 11x. In practice:

  1. Underline or group the like terms, keeping each term's sign with it.
  2. Add their coefficients.
  3. Write the result, usually with the highest power first and the constant last.
6x2−2x+9−x2+5x−4=(6x2−x2)+(−2x+5x)+(9−4)=5x2+3x+5\begin{aligned} &6x^2 - 2x + 9 - x^2 + 5x - 4 \\ &= (6x^2 - x^2) + (-2x + 5x) + (9 - 4) \\ &= 5x^2 + 3x + 5 \end{aligned}

A longer one with two variables:

3a−4b+2ab−a+7b−5ab=(3a−a)+(−4b+7b)+(2ab−5ab)=2a+3b−3ab\begin{aligned} &3a - 4b + 2ab - a + 7b - 5ab \\ &= (3a - a) + (-4b + 7b) + (2ab - 5ab) \\ &= 2a + 3b - 3ab \end{aligned}

SymPy combines like terms automatically as soon as you build the expression:

python · try it yourself
from sympy import symbols

x, a, b = symbols("x a b")
print(6*x**2 - 2*x + 9 - x**2 + 5*x - 4)
print(3*a - 4*b + 2*a*b - a + 7*b - 5*a*b)
print(x + x + x - 3*x)          # everything cancels
5*x**2 + 3*x + 5
-3*a*b + 2*a + 3*b
0

Common slips

  • Losing a sign. In 5x−3−2x5x - 3 - 2x, the term is −2x-2x, so the xx terms give 3x3x, not 7x7x.
  • Adding exponents. 2x2+3x2=5x22x^2 + 3x^2 = 5x^2, not 5x45x^4. Combining adds coefficients; the variable part stays exactly the same.
  • Merging unlike terms. x+x2x + x^2 is already simplified.

A quick check: substitute a number into the original and the simplified versions. If they disagree, there is an error.

python · try it yourself
def original(x):
    return 6*x**2 - 2*x + 9 - x**2 + 5*x - 4

def simplified(x):
    return 5*x**2 + 3*x + 5

def wrong(x):                  # added the exponents by mistake
    return 5*x**4 + 3*x + 5

for v in [0, 1, 2, -3]:
    print(v, original(v), simplified(v), wrong(v))
0 5 5 5
1 13 13 13
2 31 31 91
-3 41 41 401

The wrong version agrees at 0 and 1 by coincidence (04=020^4 = 0^2 and 14=121^4 = 1^2), which is why you should test with values like 2 and −3 as well.

In software: collecting by key

A computer algebra system combines like terms by using the variable part as a key in a dictionary and adding the coefficients that share it. You can write a miniature version in a few lines. Each term is a coefficient and a "monomial" key such as "x^2":

python · try it yourself
from collections import defaultdict

terms = [(6, "x^2"), (-2, "x"), (9, ""), (-1, "x^2"), (5, "x"), (-4, "")]
combined = defaultdict(int)
for coef, mono in terms:
    combined[mono] += coef

for mono, coef in combined.items():
    print(f"{coef:+d}{mono}")
+5x^2
+3x
+5

The same pattern totals sales per product, counts words per document and accumulates gradients per weight in machine learning frameworks: group by key, add the numbers.

Exercises

Exercise 1: Basic combining

Simplify 8y−3+2y+11−15y8y - 3 + 2y + 11 - 15y.

Answer

−5y+8-5y + 8

Solution
(8y+2y−15y)+(−3+11)=−5y+8(8y + 2y - 15y) + (-3 + 11) = -5y + 8

Exercise 2: Two variables

Simplify 4m2n−3mn2+mn2−6m2n+2mn4m^2n - 3mn^2 + mn^2 - 6m^2n + 2mn.

Answer

−2m2n−2mn2+2mn-2m^2n - 2mn^2 + 2mn

Solution
m2n terms: 4−6=−2→ −2m2nmn2 terms: −3+1=−2→ −2mn2mn term:→ 2mn\begin{aligned} &m^2n \text{ terms: } 4 - 6 = -2 && \to\ -2m^2n \\ &mn^2 \text{ terms: } -3 + 1 = -2 && \to\ -2mn^2 \\ &mn \text{ term:} && \phantom{\to\ }2mn \end{aligned}
python · try it yourself
from sympy import symbols
m, n = symbols("m n")
print(4*m**2*n - 3*m*n**2 + m*n**2 - 6*m**2*n + 2*m*n)
-2*m**2*n - 2*m*n**2 + 2*m*n

Exercise 3: Perimeter

A triangle has sides 2x+52x + 5, 3x−13x - 1 and x+4x + 4 (in cm). Write the perimeter in simplest form, then find it for x=3x = 3.

Answer

6x+86x + 8; 26 cm for x=3x = 3.

Solution
(2x+5)+(3x−1)+(x+4)=(2x+3x+x)+(5−1+4)=6x+8x=3:6⋅3+8=26check sides: 11+8+7=26 ✓\begin{aligned} &(2x + 5) + (3x - 1) + (x + 4) = (2x + 3x + x) + (5 - 1 + 4) = 6x + 8 \\ &x = 3{:}\quad 6 \cdot 3 + 8 = 26 \\ &\text{check sides: } 11 + 8 + 7 = 26\ \checkmark \end{aligned}

Exercise 4: Extend the collector

Use the dictionary method to combine [(3, "ab"), (-4, "b"), (2, "ab"), (7, "b"), (-5, "ab"), (1, "a")] and print only the terms whose coefficient is not zero.

Answer

+3b and +1a (the ab terms cancel to 0).

Solution
python · try it yourself
from collections import defaultdict
terms = [(3, "ab"), (-4, "b"), (2, "ab"), (7, "b"), (-5, "ab"), (1, "a")]
combined = defaultdict(int)
for coef, mono in terms:
    combined[mono] += coef
print([f"{c:+d}{m}" for m, c in combined.items() if c != 0])
['+3b', '+1a']

Test yourself

Which pair are like terms?

  1. 4x24x^2 and 4x4x
  2. 3xy3xy and −7yx-7yx
  3. 5a5a and 5b5b
  4. 2 and 2x2x

Simplify 3x2+4x2−x3x^2 + 4x^2 - x.

  1. 7x4−x7x^4 - x
  2. 6x26x^2
  3. 7x2−x7x^2 - x
  4. 7x37x^3

Simplify 9−2k+4k−129 - 2k + 4k - 12.

  1. 2k−32k - 3
  2. −2k+21-2k + 21
  3. 6k−36k - 3
  4. 2k+32k + 3