# 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
 1250 def combinations(c, r): 1251 # combinations('ABCD', 2) --> AB AC AD BC BD CD 1252 # combinations(range(4), 3) --> 012 013 023 123 1253 pool = tuple(range(c)) 1254 n = len(pool) 1255 if r > n: 1256 return 1257 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 break 1263 else: 1264 return 1265 indices[i] += 1 1266 for j in range(i+1, r): 1267 indices[j] = indices[j-1] + 1 1268 yield tuple(pool[i] for i in indices)
 28 def 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)
 51 def combinations(a, b): 52 """ Returns all combinations of 53 54 - fast.forwards->fast.backwards 55 - fast.forwards->slow.backwards 56 - slow.forwards->fast.backwards 57 - slow.forwards->slow.backwards 58 59 """ 60 return list(itertools.chain.from_iterable( 61 [ 62 list(itertools.product(*zip(*item))) 63 for item in correspondences(a, b) 64 ] 65 ))
 20 def all_combinations(items): 21 variants = [] 22 for n in range(len(items)+1): 23 variants.extend(itertools.combinations(items, n)) 24 return variants