Home > Blockchain >  Creating a mini program that prints all two-digit number combos w/ no repetition from certain digits
Creating a mini program that prints all two-digit number combos w/ no repetition from certain digits

Time:09-21

Can someone show me how to create a quick python program that prints all possible two digit number combos of the the digits: 1, 2, and 3. With no repetitions so no 11, 22, or 33.

CodePudding user response:

for i in range(1,4):
     for k in range(1,4):
          if i!= k:
               print(str(i) str(k))

CodePudding user response:

You can use list comprehension with condition (to exclude same digit cases).

digits = [1, 2, 3]
output = [(x, y) for x in digits for y in digits if x != y]
print(output)
# [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]

A more general and more handy way (suggested by Chris) is to use itertools.permutation:

import itertools
output = list(itertools.permutations([1, 2, 3], 2))
  • Related