from itertools import combinations code example

Example 1: python product

#from itertools import product


def product(*args, **kwds):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = map(tuple, args) * kwds.get('repeat', 1)
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

Example 2: python itertools repeat()

# How to use it:
# repeat(var, amount)

# repeat() is basically like range() but it gives an extra arg for a var

from itertools import repeat

for x in repeat("Spam? Or Eggs?", 3):
    print(x)
> "Spam? Or Eggs?"
> "Spam? Or Eggs?"
> "Spam? Or Eggs?"

Example 3: python create a program that runs through all possible combinations

from itertools import combinations

lst = ["a" ,"b", "c"]
lengthOfStrings = 3
for i in combinations(lst, lengthOfStrings):
  print(i)

Example 4: python combinations function

def combinations(iterable, r):
    pool = tuple(iterable)
    n = len(pool)
    for indices in permutations(range(n), r):
        if sorted(indices) == list(indices):
            yield tuple(pool[i] for i in indices)

Example 5: from itertools import combinations

from itertools import combinations
from itertools import permutations