-1

Hi I would like to knoq how I can achieve this in python.

given: num=4

expect: (0,1), (0,2), (0,3), (0,4), (1,2), (1,3), (1,4), (2,3), (2,4), (3,4) (in tuple)

cyntha
  • 113
  • 7
  • What have you tried so far? Here's a hint - create a range up to your number + 1 and then use `itertools.product` to get a Cartesian product of the two copies of that range. – NotAName Mar 07 '23 at 00:35

1 Answers1

1

Using nested loops:

>>> num = 4
>>> for i in range(num):
...     for j in range(i+1, num+1):
...         print((i, j))
...
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)

Using itertools:

>>> import itertools
>>> print(*itertools.combinations(range(num+1), 2), sep="\n")
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)
Samwise
  • 68,105
  • 3
  • 30
  • 44