the itertools and collections modules combinations if we want all the possible -element subsets from sequencewhere all that matters is the elementsnot the order in which they appearthen what we want are combinations for instancethe -element subsets that can be made from { are { }{ and { we consider { and { as being the same because they contain the same elements here is an example showing the combinations of two-element substrings possible from ' ''join(cfor in combinations' ' )[' '' '' 'combinations with replacement for combinations with repeated elementsuse the function combinations_with_replacement 'join(cfor in combinations_with_replacement' ' )[' '' '' '' '' '' ' cartesian product the function product produces an iterator from the cartesian product of iterables the cartesian product of two sets and consists of all pairs (xywhere is in and is in here is short example'join(pfor in product'abc '' ')[' '' '' '' '' '' '' '' '' 'example to demonstrate the use of producthere are three progressively shorterand clearer ways to find all oneor two-digit pythagorean triples (values of (xyzsatisfying the first way uses nested for loopsfor in range( , )for in range( , )for in range( , )if ** + ** == ** print( , ,zhere is the same thing rewritten with productx range( , for ( , ,zin product ( , , )if ** + ** == ** print( , ,zit is even shorter if we use list comprehensionsx range( , [( , ,zfor ( , ,zin product( , ,xif ** + ** == **