-2

This is the list given but can be any number of integers

list = [2,3,5,6]

This should be the outcome:

(2,2)
(2,3)
(2,5)
(2,6)
(3,2)
(3,3)
(3,5)
(3,6)
(5,2)
(5,3)
(5,5)
(5,6)
(6,2)
(6,3)
(6,5)
(6,6)
dangee1705
  • 3,445
  • 1
  • 21
  • 40
  • have a look at `itertools` – dangee1705 Mar 30 '20 at 23:32
  • 1
    Does this answer your question? [Operation on every pair of element in a list](https://stackoverflow.com/questions/942543/operation-on-every-pair-of-element-in-a-list) – AMC Mar 30 '20 at 23:53

2 Answers2

1

You can use itertools

list(itertools.product(l, repeat=2))
[(2, 2),
 (2, 3),
 (2, 5),
 (2, 6),
 (3, 2),
 (3, 3),
 (3, 5),
 (3, 6),
 (5, 2),
 (5, 3),
 (5, 5),
 (5, 6),
 (6, 2),
 (6, 3),
 (6, 5),
 (6, 6)]
J1nn
  • 36
  • 3
  • 1
    firstly, you shouln't give answers to such low quality questions because it encourages more of them. secondly, it would be better to use `itertools.product` – dangee1705 Mar 30 '20 at 23:34
  • 1
    Where's `(3,2)`, `(5,2)`, etc...? Combinations is not what the OP wants based on their desired outcome. – Mark Mar 30 '20 at 23:42
1

The solution could be:

[(i,j) for i in list for j in list]

which is the "list-comprehension" version of:

result = []
for i in list:
   for j in list:
       result.append((i,j))

Please note that using list = something you are overriding a python keyword.

enneppi
  • 1,029
  • 2
  • 15
  • 33