# 4 examples of 'itertools combinations' in Python

Every line of 'itertools combinations' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

## All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
``1250def combinations(c, r):1251    # combinations('ABCD', 2) --&gt; AB AC AD BC BD CD1252    # combinations(range(4), 3) --&gt; 012 013 023 1231253    pool = tuple(range(c))1254    n = len(pool)1255    if r &gt; n:1256        return1257    indices = list(range(r))1258    yield tuple(pool[i] for i in indices)1259    while True:1260        for i in reversed(list(range(r))):1261            if indices[i] != i + n - r:1262                break1263        else:1264            return1265        indices[i] += 11266        for j in range(i+1, r):1267            indices[j] = indices[j-1] + 11268        yield tuple(pool[i] for i in indices)``
``28def testCombinations(self):29    for i in range(15):30        case = range(i)31        k = random.randrange(len(case) + 1)32        result = combinations(case, k)33        result = set(map(tuple, result))34        expect = itertools.combinations(case, k)35        expect = set(expect)36        self.assertEqual(result, expect)``
``51def combinations(a, b):52    """ Returns all combinations of5354     - fast.forwards-&gt;fast.backwards55     - fast.forwards-&gt;slow.backwards56     - slow.forwards-&gt;fast.backwards57     - slow.forwards-&gt;slow.backwards5859    """60    return list(itertools.chain.from_iterable(61        [62            list(itertools.product(*zip(*item)))63            for item in correspondences(a, b)64        ]65    ))``
``20def all_combinations(items):21    variants = []22    for n in range(len(items)+1):23        variants.extend(itertools.combinations(items, n))24    return variants``