Search code examples
pythonpermutationpython-itertools

How to use itertools to output results of only a certain length


Suppose I have a list of bytes (x00 to xFF). How can I use itertools to return only permutations that have length of X. So for example I want all permutations with length 3, then I'll get

[x00,x00,x00], [x00,x00,x01], ..., [xFF,xFF,xFF]

That way there is no waste of computing resources.

EDIT: Doesn't have to be itertools if there is a better way.


Solution

  • import itertools
    for tup in itertools.product(range(0x100), repeat=3):
        ...