itertools.product() vs itertools.permutations()
itertools.product() and itertools.permutations() are both Python Itertools Module APIs, and choosing between them is a common point of confusion. This page compares their syntax, parameters, return values, and typical use cases so you can pick the right one. itertools.product(): Cartesian product of input iterables (like nested for-loops). Its syntax is itertools.product(*iterables, repeat=1), and it returns iterator of tuples itertools.permutations(): Returns all possible r-length ordered arrangements of elements. Its syntax is itertools.permutations(iterable, r=None), and it returns iterator of tuples Example of itertools.product(): list(product([1,2], ["a","b"]))
# [(1,"a"), (1,"b"), (2,"a"), (2,"b")]. Example of itertools.permutations(): list(permutations([1,2,3], 2))
# [(1,2),(1,3),(2,1),(2,3),(3,1),(3,2)]. Read the full references for itertools.product() and itertools.permutations() to see parameters, edge cases, and example code side by side. Other Itertools Module APIs worth comparing: itertools.chain(), itertools.chain.from_iterable(), itertools.combinations(), itertools.combinations_with_replacement(), itertools.cycle(), itertools.repeat(). Both belong to the Itertools Module category in the Python Reference documentation. Try both in the Python playground to see the behavioral difference live.