Search code examples
pythonpython-itertools

How can I generate all possible combinations of numbers?


I am currently working a python script that would generate all the possible combinations of numbers. I am using the itertools module for this. My problem is: I don´t get doubled numbers in the results. For example: 245325 or 111222. Here is my current code:

from itertools import permutations

comb = permutations([1, 2, 3, 4, 5, 6, 7, 8, 9, 0], 6)

f = open("code.txt", "w")

for i in comb:
    nums = str(i)
    cleared = nums.translate({ord(nums): None for nums in ',() '})
    print(cleared)
    f.write(cleared + "\n")


Solution

  • a single line

    list(itertools.product(range(10), repeat=6))
    

    or based on nigh_anxiety comment

    list(range(100000, 1000000))