combinations python itertools code example
Example 1: 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 2: 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)