Enumerate with itertools
Products, combinations, and permutations.
Enumerate with itertools is a free Coding Interview Prep lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Let itertools Do the Loops
Python's itertools generates combinations and orderings for you, so you write less loop code and make fewer mistakes. 🙂
Import It First
Everything lives in one module, so start with import itertools. Then you can call product, permutations, and combinations directly.
import itertoolsproduct for Nested Loops
Use product to replace deeply nested loops over the same choices. It yields every tuple of picks across the given ranges.
for combo in itertools.product(range(3), repeat=2):
print(combo)permutations for Orderings
When order matters, permutations lists every arrangement of the items. Perfect for tiny traveling-salesman style brute force.
for p in itertools.permutations([1, 2, 3]):
print(p)combinations for Choosing
When order does not matter, combinations picks every group of size r. Great for choosing a subset of items to test.
for c in itertools.combinations([1, 2, 3, 4], 2):
print(c)Permutations vs Combinations
The key difference is order: permutations treats (1,2) and (2,1) as distinct, while combinations counts them as the same pick.
Mind the Growth
Permutations grow as n!, which explodes fast. Even n equal to 11 already exceeds forty million arrangements, so keep n small.
They Are Lazy
These functions are generators: they yield one item at a time without building a giant list, so memory stays low while you loop.
Combine With a Check
Generate candidates, then keep only the ones that pass your test. This pattern turns a hard search into a short readable loop.
best = min(itertools.permutations(cities), key=tour_length)combinations_with_replacement
Need to pick items that can repeat? Use combinations_with_replacement to allow the same element more than once in a group.
Pick the Right Tool
Ask two questions: does order matter, and can items repeat? Those answers point you straight to the correct itertools function.
Quick Check
You must enumerate every way to pick 3 of 6 items where order does not matter.
Recap
Use product, permutations, and combinations to enumerate cleanly. Choose by asking if order matters and if items repeat, and watch the growth. 🚀
Frequently asked questions
Is the “Enumerate with itertools” lesson free?
Yes — the full text of “Enumerate with itertools” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.
What will I learn in “Enumerate with itertools”?
Products, combinations, and permutations. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Coding Interview Prep?
No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Enumerate with itertools” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Coding Interview Prep lesson?
Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Brute Force Is a Valid Strategy
- Enumerate with itertools
- Bitmask Subset Enumeration
- Trim the Search Space Smartly