-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
  • 529
  • 1
  • 4
  • 11

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,650
  • 2
  • 13
  • 25
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