Generating all 5 card poker hands
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Generating every 5-card poker hand is a classic combinations problem: choose 5 distinct cards from a 52-card deck, ignoring order. The important word is combinations, not permutations, because a hand containing the same five cards is the same hand regardless of dealing order.
Build a Standard 52-Card Deck
The first step is to represent the deck. A simple Python representation uses rank-suit strings:
Each card is unique, which is exactly what you need before generating hands.
Use Combinations, Not Nested Loops by Hand
The mathematical count is:
C(52, 5) = 2,598,960
Programmatically, the direct way to enumerate them is itertools.combinations:
This yields one 5-card hand at a time. The tuples are generated lazily, so Python does not store all 2,598,960 hands in memory at once.
That laziness is a big deal. It makes full enumeration feasible for analysis and simulation workflows.
Count Hands Without Materializing Them
If you only need the number of hands, use the combinatorics directly:
This is far faster than iterating through every hand just to count them.
Use full generation only when you actually need to inspect, score, or classify each hand.
That distinction matters in probability work, where the total count is often the answer, while simulation or classification tasks need the actual hand generator.
Process Hands One by One
A common pattern is to generate each hand and immediately analyze it:
This approach is memory-friendly because each hand is processed and discarded immediately.
Why Order Does Not Matter
One common bug is generating permutations instead of combinations. A hand containing AS, KD, 10H, 5C, 2S should be counted once, not once for every ordering of those five cards.
That is why combinations(deck, 5) is the right tool and permutations(deck, 5) is not.
The difference is enormous. Five-card permutations from a 52-card deck number 311,875,200, which is not the same problem at all.
Common Pitfalls
The biggest mistake is building all hands into a list immediately:
That works on many machines, but it uses much more memory than necessary and is usually the wrong default.
Another common issue is forgetting that rank strings like "10" are two characters while suit is one. If you parse cards by slicing, make sure you are extracting the rank and suit consistently.
It is also easy to use permutations by accident and silently multiply the search space far beyond the actual number of unique poker hands.
Summary
- A 5-card poker hand is a combination of 5 cards from 52, not a permutation.
- There are
2,598,960unique 5-card hands. - Use
itertools.combinations(deck, 5)to generate them lazily. - Use
math.comb(52, 5)if you only need the count. - Process each hand as it is generated instead of storing every hand in memory unless you truly need them all at once.
Related reading
- Generating All Combinations of List n Levels Deep in Java
- Generating all factors of a number given its prime factorization
- Generating all permutations excluding cyclic rotations
- Generating all permutations of a given string
- Generating all permutations of a given string
- Generating permutations lazily
- Generating permutations of a set most efficiently
- Generating random numbers under very specific constraints

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.