Python itertools Full Beginner Guide

๐Ÿ Complete Guide to Python itertools — Permutations, Combinations and Subsets Explained

The itertools module in Python is one of the most powerful tools for working with permutations, combinations, subsets (power sets), Cartesian products and more.

In this guide, we’ll understand:

  • ✔️ What are permutations?
  • ✔️ What are combinations?
  • ✔️ What are subsets?
  • ✔️ How itertools functions work internally
  • ✔️ Differences between each

This post is perfect for students, programmers, competitive coders, and data scientists.


๐Ÿ“˜ What Are Permutations?

Permutation = Arrangement where order matters.
Example: For digits [1, 2, 3], permutations of length 2:

# Order matters
(1, 2)  ≠  (2, 1)

Formula: nPr = n! / (n-r)!

๐Ÿงช Code: Generating Permutations


from itertools import permutations

nums = [1, 2, 3]
perms = list(permutations(nums, 2))

print("All permutations of length 2:")
for p in perms:
    print(p)

# Output:
# (1, 2)
# (1, 3)
# (2, 1)
# (2, 3)
# (3, 1)
# (3, 2)

⚙️ How permutations() works internally

The function returns an iterator that produces all possible ordered arrangements of the chosen elements. It does NOT reuse elements unless you tell it (use product for repetition).


๐Ÿ“— What Are Combinations?

Combination = Selection where order does NOT matter.


# Order does NOT matter
(1, 2)  ==  (2, 1)

Formula: nCr = n! / r! (n-r)!

๐Ÿงช Code: Combinations Example


from itertools import combinations

nums = [1, 2, 3]
combs = list(combinations(nums, 2))

print("Combinations of length 2:")
for c in combs:
    print(c)

# Output:
# (1, 2)
# (1, 3)
# (2, 3)

✨ combinations_with_replacement()

Allows picking the same element more than once.


from itertools import combinations_with_replacement

nums = [1, 2, 3]
crep = list(combinations_with_replacement(nums, 2))

print("Combinations with repetition:")
for c in crep:
    print(c)

# Output:
# (1, 1)
# (1, 2)
# (1, 3)
# (2, 2)
# (2, 3)
# (3, 3)

⚙️ How combinations() works internally

The function picks r items at a time, but does NOT change their order. It treats (1,2) and (2,1) as the same combination, so it only returns one.


๐Ÿ“™ What Are Subsets (Power Sets)?

A subset / power set contains all possible selections of items:


[1,2,3] subsets =
()  
(1)
(2)
(3)
(1,2)
(1,3)
(2,3)
(1,2,3)

We generate subsets using combinations in a loop.

๐Ÿงช Code: Power Set Function


from itertools import combinations

def powerset(iterable):
    items = list(iterable)
    result = []
    for r in range(len(items)+1):
        for combo in combinations(items, r):
            result.append(combo)
    return result

print("Power set of [1,2,3]:")
for s in powerset([1,2,3]):
    print(s)

๐Ÿ” Differences at a Glance (Comparison Table)

Operation Function Order Matters? Repetition Allowed? Example
Permutations permutations() Yes No (1,2) ≠ (2,1)
Combinations combinations() No No (1,2) == (2,1)
CWR (repetition) combinations_with_replacement() No Yes (1,1), (1,2)…
Power Set loop of combinations() No No All subsets

๐Ÿ“ข Join Our Coding Community

  • Join our WhatsApp community for Python automation, data structures, and coding tutorials.

❤️ If this post helped you, share it with fellow Python learners!

Previous Post Next Post

Contact Form