How to use 'permutation in python without itertools' in Python

Every line of 'permutation in python 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
this disclaimer
24@cython.locals(n=int, i=int, j=int)
25def permutations(iterable):
26 """permutations(range(3), 2) --> (0,1) (0,2) (1,0) (1,2) (2,0) (2,1)"""
27 pool = tuple(iterable)
28 n = len(pool)
29 indices = list(range(n))
30 cycles = list(range(1, n+1))[::-1]
31 yield [ pool[i] for i in indices ]
32 while n:
33 for i in reversed(range(n)):
34 j = cycles[i] - 1
35 if j == 0:
36 indices[i:] = indices[i+1:] + indices[i:i+1]
37 cycles[i] = n - i
38 else:
39 cycles[i] = j
40 indices[i], indices[-j] = indices[-j], indices[i]
41 yield [ pool[i] for i in indices ]
42 break
43 else:
44 return
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code


Related snippets