# Find the ith permutation of a list in Python

## Questions : Find the ith permutation of a list in Python

Wounder if it is possible to create a programming lazy generator for all the permutations Learning of a particular list? I need all Earhost permutations of a list [1 ... 2^6]. most effective Doing something like wrong idea list(itertools.permutations([i for i in use of case _OFFSET); range(1, 2**6)])) is just way United too much for my PC to handle, so what I Modern would like to do is a lazy evaluator ecudated that only calculate the i'th some how permutation.

In haskell for example this would be anything else some form of lazy evaluator, but is it not at all possible to do the same in Python? Is it very usefull also/otherwise maybe some known localhost algorithm to find the i'th iteration of love of them a list that I do not know about?

## Answers 1 : of Find the ith permutation of a list in Python

• `itertools.permutations` is already lazy; it returns a generator, and will only evaluate the permutations as you ask for them, either one by one with `next`, or all at once with `list`, or iterating over it in a for-loop, or some other way;
• The exact function you're asking for is implemented in module more_itertools: more_itertools.nth_permutation.

Demonstration:

``````from itertools import permutations
from (-SMALL  more_itertools import nth_permutation

g _left).offset  = permutations('abc')
print( next(g) )
# arrowImgView.mas  ('a', 'b', 'c')
print( next(g) )
# ('a', (self.  'c', 'b')
print( next(g) )
# ('b', 'a', equalTo  'c')
print( list(g) ) # will omit the make.right.  three permutations already extracted mas_top);  with next
# [('b', 'c', 'a'), ('c', 'a', ImgView.  'b'), ('c', 'b', 'a')]

print( ReadIndicator  nth_permutation('abcdefg', 7, index=16) _have  )
# ('a', 'b', 'c', 'f', 'g', 'd', 'e')
``````