Advertisement
Permutations and combinations usually show up in school math, often wrapped in formulas and theory. But in Python, they become hands-on tools. You’ll see them used in generating test cases, solving simple puzzles, or working with structured data. Python makes them easy to use, even for those who aren’t math-heavy. Once you get what they mean—one’s about arrangements, the other about selections—you can use them to solve real problems. No need to calculate anything by hand; Python handles the math, and you just guide the logic.
A permutation is an arrangement where the order matters. So, ['a', 'b'] and ['b', 'a'] are two different permutations. A combination is a selection where order doesn’t matter—['a', 'b'] and ['b', 'a'] count as one.
These ideas come up in programming more than you’d expect. From seating charts to generating possible keys or organizing choices, they offer ways to list or filter possibilities. Instead of writing heavy logic, Python’s itertools module simplifies this.
The itertools module is built into Python and includes permutations() and combinations(). These functions return iterators—memory-friendly sequences that generate results as needed. You can loop through them or convert them to lists.
Take a list like ['a', 'b', 'c']. Using itertools.permutations(data) gives you all six possible ordered arrangements: abc, acb, bac, bca, cab, cba. But itertools.combinations(data, 2) gives you only three pairings: ab, ac, bc—since it doesn’t care about order.
Python handles the calculation, so you don’t have to. Instead of plugging numbers into formulas, you work directly with the data.

Start by importing the right function:
from itertools import permutations
data = ['a', 'b', 'c']
perm = permutations(data)
for p in perm:
print(p)
This prints every way to arrange the three items. If you want shorter sets, like two-letter results, add a second argument:
perm = permutations(data, 2)
That returns: ab, ac, ba, bc, ca, cb. Each result is a tuple. To use it elsewhere, such as joining strings, you can use "".join(p).
Permutations are a basic part of combinatorics, often used in puzzles, pattern tests, or generating all outcomes. The results grow fast, though. Five items produce 120 permutations. Ten items give over 3 million. So be aware of scale.
Python’s approach makes these easier. You skip the manual work and focus on what the task needs. Whether you’re generating IDs, solving puzzles, or checking every combination of steps, Python makes it manageable.
Combinations ignore order, so ['a', 'b'] and ['b', 'a'] are treated as the same.
from itertools import combinations
data = ['a', 'b', 'c']
comb = combinations(data, 2)
for c in comb:
print(c)
The output: ('a', 'b'), ('a', 'c'), ('b', 'c'). These are helpful for selections where the order doesn't change the meaning—such as picking team members or product sets.
As with permutations, you get tuples, and you can convert or format them as needed.
There’s also combinations_with_replacement(), which allows repeated elements, such as ('a', 'a'). This can help in simulations, repeated selections, or modeling choices where repeats are allowed.
Combinations reduce the total results compared to permutations. That helps when testing different feature sets or choosing variable groups in machine learning. It avoids checking every possible order, which saves time and memory.
Still, the total can grow large. But since itertools uses iterators, you only load items as needed. That keeps things efficient and lets your code scale well.
In real tasks, permutations and combinations come in handy. You may need to test every password pattern, solve a word game, or list every possible outcome of a set of inputs. In interviews, you might be asked to generate anagrams or form all group combinations. Python allows you to handle these tasks without complex code.

Some try to use nested loops for small examples. That’s fine for short inputs. But with larger sets, it breaks down fast. Using itertools keeps your logic concise and clean, and it handles larger datasets more reliably.
You might also try writing your permutation function using recursion. It's a good way to understand the logic, but for day-to-day use, stick with built-in tools—they're faster and less prone to errors.
Duplicates can be tricky. If your list includes repeats like ['a', 'a', 'b'], then permutations() include repeat results. To get unique outputs, convert to a set:
unique_perms = set(permutations(['a', 'a', 'b']))
If you don’t need every result, use slicing to limit output. For example, get only the first 100 permutations:
from itertools import islice
for p in islice(permutations(data), 100):
print(p)
This helps when you're exploring ideas or testing subsets. You can also add filters to skip invalid or unwanted outputs.
In places like game design, simulations, or analysis tools, these methods help generate outcomes, test rules, or find the most efficient combinations. You might explore every sequence of a puzzle or examine groupings of players, moves, or data sets. These aren’t just academic—they help organize real decisions and explore possibilities.
Combinatorics doesn’t just mean solving math problems. With Python, it becomes a set of working tools. You list things, explore patterns, simulate situations, and analyze combinations—all using a few lines of code.
Permutation and combination in Python are easier than their math versions. Once you understand the difference—whether order matters or not—the rest is simple. Python's tools, such as itertools, make it practical. You can work through puzzles, create test cases, or simulate real-life setups without deep formulas or lengthy calculations. Whether you're dealing with passwords, names, choices, or structures, this approach handles it cleanly. You don't have to write extra logic or guess outcomes. Python does the work; you steer the direction. Combinatorics in Python becomes less about theory and more about solving everyday programming tasks effectively and efficiently.
Advertisement
Discover the top data science leaders to follow in 2025. These voices—from educators to machine learning experts—shape how real-world AI and data projects are built and scaled
Need to deploy a 405B-parameter Llama on Vertex AI? Follow these steps for a smooth deployment on Google Cloud
Policymakers analyze AI competition between the U.S. and China following DeepSeek’s significant breakthroughs.
Hugging Face enters the world of open-source robotics by acquiring Pollen Robotics. This move brings AI-powered physical machines like Reachy into its developer-driven platform
Gemma 3 mirrors DSLMs in offering higher value than LLMs by being faster, smaller, and more deployment-ready
How CPU Optimized Embeddings with Hugging Face Optimum Intel and fastRAG can run fast, low-cost RAG pipelines without GPUs. Build smarter AI systems using Intel Xeon CPUs
Looking for the best cloud GPU providers for 2025? Compare pricing, hardware, and ease of use from trusted names in GPU cloud services
Why INDEX MATCH is often a better choice than VLOOKUP in Excel. Learn the top 5 reasons to use INDEX MATCH for more flexible, efficient, and reliable data lookups
Explore the Python strftime() function and how it helps convert datetime objects into formatted strings. Learn common usage, tips, and avoid pitfalls in this detailed guide
Discover how Nvidia continues to lead global AI chip innovation despite rising tariffs and international trade pressures.
IBM AI agents boost efficiency and customer service by automating tasks and delivering fast, accurate support.
How to use the Python time.sleep() function with clear examples. Discover smart ways this sleep function can improve your scripts and automate delays