poker
card games
probability
combinatorics
game theory

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.

Practice algorithms

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:

python
1from itertools import combinations
2
3ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
4suits = ["C", "D", "H", "S"]
5
6deck = [f"{rank}{suit}" for suit in suits for rank in ranks]
7print(len(deck))  # 52

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:

python
1from itertools import combinations
2
3all_hands = combinations(deck, 5)
4
5for i, hand in enumerate(all_hands):
6    if i < 5:
7        print(hand)
8    else:
9        break

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:

python
import math

print(math.comb(52, 5))  # 2598960

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:

python
1from collections import Counter
2from itertools import combinations
3
4
5def rank_pattern(hand):
6    ranks = [card[:-1] for card in hand]
7    counts = sorted(Counter(ranks).values(), reverse=True)
8    return tuple(counts)
9
10
11pair_count = 0
12for hand in combinations(deck, 5):
13    if rank_pattern(hand) == (2, 1, 1, 1):
14        pair_count += 1
15
16print(pair_count)

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:

python
hands = list(combinations(deck, 5))

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,960 unique 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.