Permutations and combinations
Permutations and combinations are counting rules: a permutation counts the ordered arrangements of r items chosen from n, and a combination counts the unordered selections.
Last updated: 07 Oct, 2026 · SciPy 1.18
Every probability in Probability basics was a count of outcomes. With a die you can list them, but with 6 chocolates, 52 cards or 100 coin tosses you need a formula for the count. Permutations and combinations are those formulas.
Counting ordered lists of chocolates
The video's school trip goes to a chocolate factory that makes 6 kinds: Dairy Milk, 5 Star, Milky Bar, Eclairs, Gems and Silk. A student's assignment is to write down the first three chocolates they see, in the order they see them.
The first name can be any of 6 chocolates. Once it is written, 5 are left for the second name, then 4 for the third. So there are 6 × 5 × 4 = 120 possible lists. "Dairy Milk, Gems, Milky Bar" and "Milky Bar, Gems, Dairy Milk" use the same chocolates but are different lists. Each ordered list is a permutation.
Here n = 6 is the number of chocolates and r = 3 is the number of names written. n! (n factorial) is n × (n − 1) × … × 1, and the (n − r)! cancels the part of 6! that is never used.
Counting unordered groups of chocolates
In a combination only the group matters, not the order. Dairy Milk, Gems and Eclairs is one combination however the three are arranged; swapping them around does not make a new one.
The two counts are tied together. Each group of three can be put in order in 3! = 3 × 2 × 1 = 6 ways, so each group appears 6 times among the 120 lists. Dividing removes the repeats: 120 ÷ 6 = 20. In general nCr = nPr ÷ r!.
The question decides which to use. "The first three chocolates you see, in order" is a permutation. "Which three chocolates go into a gift box" is a combination. Both formulas assume n different items and no item used twice. When repeats are allowed, there are nʳ ordered lists (6³ = 216) and C(n + r − 1, r) unordered ones (C(8, 3) = 56).
Counting with math and itertools
math.perm and math.comb give the counts directly; itertools builds the lists themselves, so the counts can be checked one by one.
import math
from itertools import permutations, combinations, combinations_with_replacement
chocolates = ["Dairy Milk", "5 Star", "Milky Bar", "Eclairs", "Gems", "Silk"]
print("6P3 =", math.perm(6, 3), " 6C3 =", math.comb(6, 3), " 3! =", math.factorial(3))
lists = list(permutations(chocolates, 3)) # ordered
groups = list(combinations(chocolates, 3)) # unordered
print("ordered lists:", len(lists), " groups:", len(groups), " 120 / 6 =", len(lists) // 6)
print("first two lists:", lists[:2])
one = {"Dairy Milk", "Gems", "Eclairs"}
print("lists using exactly that group:", sum(1 for l in lists if set(l) == one))
print("with repeats, ordered:", 6 ** 3, " unordered:", len(list(combinations_with_replacement(chocolates, 3))))
print("P(a random group of 3 is that group) =", 1 / math.comb(6, 3))
print("5-card hands from 52 cards:", math.comb(52, 5))6P3 = 120 6C3 = 20 3! = 6
ordered lists: 120 groups: 20 120 / 6 = 20
first two lists: [('Dairy Milk', '5 Star', 'Milky Bar'), ('Dairy Milk', '5 Star', 'Eclairs')]
lists using exactly that group: 6
with repeats, ordered: 216 unordered: 56
P(a random group of 3 is that group) = 0.05
5-card hands from 52 cards: 2598960What the chocolate counts show
- 6P3 = 120 and 6C3 = 20: the two counts worked out on the board.
- itertools builds 120 lists and 20 groups, and 120 ÷ 6 = 20 matches.
- The group {Dairy Milk, Gems, Eclairs} appears in 6 of the lists: its 3! orderings.
- Repeats change the counts: 216 ordered lists and 56 groups when a chocolate may appear more than once.
- A random group is that one group with probability 0.05, 1 out of 20, and there are 2,598,960 five-card poker hands.
Permutation vs combination
| Permutation | Combination | |
|---|---|---|
| Order matters? | Yes | No |
| Formula | n! ÷ (n − r)! | n! ÷ (r! (n − r)!) |
| 6 chocolates, choose 3 | 120 | 20 |
| Question it answers | How many ordered lists or rankings? | How many groups or teams? |
| Python | math.perm(n, r) | math.comb(n, r) |
Where you use permutations and combinations
- The binomial distribution: C(n, k) counts the ways to get k heads in n tosses, the first step of the Binomial distribution formula.
- Odds of a draw: lottery tickets, card hands and passwords are counts of combinations or permutations.
- Model search: choosing 3 of 10 features gives C(10, 3) = 120 subsets to try.
Related
- Previous: Conditional probability and Bayes' theorem
- Next: Random variables and probability distributions
- A race has 8 runners. Count the ways to award gold, silver and bronze with
math.perm(8, 3), and the ways to pick 3 finalists withmath.comb(8, 3). - Check that
math.comb(6, 2) == math.comb(6, 4): both are 15. Why does choosing 2 chocolates to take equal choosing 4 to leave behind? - Print
len(list(permutations(chocolates))). Is it 6! = 720?
Every expert started right here.