Find Number Combinations in Python
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.
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
// 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] All orderings of three numbers
Order matters here, so this is permutation listing.
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() All pairs from four numbers
Order does not matter in this pair listing, so we use j = i + 1.
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
🔄 Input / output
Examples use fixed lists. For interactive mode, parse user input into a list and keep the same loop logic.
Edge cases
Repeated values
Distinct indices can still print identical value triples when input has duplicates.
Too few elements
For pair listing, if n < 2 there are no pairs to print.
⏱️ Time and space complexity
| Program | Time | Extra space |
|---|---|---|
| Triple nested loops (fixed size 3) | O(1) for fixed input | O(1) |
| Pairs from n elements | O(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.
In math, a combination ignores order, while a permutation treats different orders as different results. This page demonstrates both patterns using simple nested loops.
8 people found this page helpful
