-2

I have a list that contains 16 unique numbers:

list = [1, 2, 3, 4, 5, 6, 7,...,16]

I want to iterate over all possible combinations of 4 of the numbers, eg:

iterate1 = [1, 2, 3, 4]
iterate2 = [1, 2, 3, 5]
iterate3 = [1, 2, 3, 6]
.
.
.

Numbers in the list cannot repeat.

Mike
  • 619
  • 1
  • 5
  • 13

2 Answers2

0

I think you want itertools

import itertools
list(itertools.permutations([1,2,3...16],4)

EDIT

Or if you actually need the combination function, just use that.

import itertools
list(itertools.combinations([1,2,3...16],4)
SuperStew
  • 2,857
  • 2
  • 15
  • 27
0
import itertools
i = 1
print i
for x in itertools.combinations(range(1,16), 4) :
    print "iterate" + str(i) + " = " +  str(x)
    i = i + 1
UchihaItachi
  • 2,602
  • 14
  • 21