# 7 examples of 'python combinations without itertools' in Python

Every line of 'python combinations without itertools' 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``
``98async def combinations(itr: AnyIterable[T], r: int) -&gt; AsyncIterator[Tuple[T, ...]]:99    """100    Yield r length subsequences from the given iterable.101102    Simple wrapper around itertools.combinations for asyncio.103    This will consume the entire iterable before yielding values.104105    Example:106107        async for value in combinations(range(4), 3):108            ...  # (0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)109110    """111    pool: List[T] = await list(itr)112    for value in itertools.combinations(pool, r):113        yield value``
``180def xuniqueCombinations(items, n):181    if n == 0:182        yield []183    else:184        for i in xrange(len(items)):185            for cc in xuniqueCombinations(items[i + 1:], n - 1):186                yield [items[i]] + cc``
``26def xuniqueCombinations(items, n):27    if n==0: yield []28    else:29        for i in xrange(len(items)):30            for cc in xuniqueCombinations(items[i+1:],n-1):31                yield [items[i]]+cc``