Find Number Combinations in Python

Beginner
⏱️ 10 min read
📚 Updated: May 2026
🎯 2 Code Examples
Nested loops

What you’ll learn

  • How to list all orderings of three numbers (permutations).
  • How to list all unique pairs from four numbers (combinations).
  • How index conditions avoid duplicates.

Overview

This tutorial shows two related patterns: order-sensitive triples and order-insensitive pairs.

Permutations

All six orderings for three distinct values.

Pairs

All unique pairs using index rule j > i.

Live preview

Type three integers and list all orderings.

Prerequisites

Python loops, list indexing, and simple conditions.

  • Know list access with arr[i].
  • Understand that order may or may not matter depending on problem statement.

The idea

Example 1 fills three slots in all possible distinct index ways. Example 2 picks two indices with i < j so each pair appears once.

Nested loops are enough for small interview-size datasets.

Live preview

Enter three integers (e.g. 1, 2, 3) and list all orderings.

Use commas or spaces. Duplicate values may cause repeated-looking lines.

Live result
Press "List orderings".

Algorithm

Goal: print permutations of 3 numbers and unordered pairs from 4 numbers.

Store values in a list

Use fixed sample values for clear output.

Use nested loops

Apply distinct-index or j > i rules as needed.

Print each valid selection

One line per valid tuple/pair.

📜 Pseudocode

Pseudocode
// Permutations of 3
for i in 0..2:
  for j in 0..2:
    for k in 0..2:
      if i, j, k are all distinct:
        print arr[i], arr[j], arr[k]

// Unordered pairs
for i in 0..n-2:
  for j in i+1..n-1:
    print arr[i], arr[j]
1

All orderings of three numbers

Order matters here, so this is permutation listing.

python
def print_three_permutations(arr: list[int]) -> None:
    print("All orderings (permutations) of the three numbers:")
    for i in range(3):
        for j in range(3):
            for k in range(3):
                if i != j and j != k and i != k:
                    print(arr[i], arr[j], arr[k])

def main() -> None:
    arr = [1, 2, 3]
    print_three_permutations(arr)

if __name__ == "__main__":
    main()
2

All pairs from four numbers

Order does not matter in this pair listing, so we use j = i + 1.

python
def print_unordered_pairs(arr: list[int]) -> None:
    n = len(arr)
    print(f"All unordered pairs (choose 2 from {n}):")
    for i in range(n):
        for j in range(i + 1, n):
            print(arr[i], arr[j])

def main() -> None:
    arr = [10, 20, 30, 40]
    print_unordered_pairs(arr)

if __name__ == "__main__":
    main()

Notes

Bigger input sizes. For many elements, use dedicated algorithms or modules rather than hand-written deep loops.

Duplicate values. Repeated values may create repeated-looking lines; add filtering if unique output values are required.

❓ FAQ

This page shows two patterns: all orderings of three numbers (permutations), and all unordered pairs from four numbers (combinations).
Each loop picks one position in the triple. Distinct index checks ensure one array slot is not reused in the same line.
There are 3! = 6 permutations.
It avoids duplicates like both (10,20) and (20,10). Each unordered pair appears once.
Yes. Example 2 is choose 2 from 4. Loops list each subset explicitly.
Pair generation is O(n^2). Triple nested loops over n elements are O(n^3).

🔄 Input / output

Examples use fixed lists. For interactive mode, parse user input into a list and keep the same loop logic.

Edge cases

Duplicates

Repeated values

Distinct indices can still print identical value triples when input has duplicates.

Small n

Too few elements

For pair listing, if n < 2 there are no pairs to print.

⏱️ Time and space complexity

ProgramTimeExtra space
Triple nested loops (fixed size 3)O(1) for fixed inputO(1)
Pairs from n elementsO(n^2)O(1)

Summary

  • Permutations: distinct i, j, k indices give all orderings.
  • Combinations: j > i avoids duplicate pair reversals.
  • Concept: order matters for permutations, not for combinations.
Did you know?

In math, a combination ignores order, while a permutation treats different orders as different results. This page demonstrates both patterns using simple nested loops.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful