How to find all possible combinations of X numbers from a list of N numbers in Python

2 Answers

0 votes
from itertools import combinations 
  
X = 3
lst = [1, 2, 3, 4]
comb = combinations(lst, X) 

for ol in list(comb): 
    print(ol)



'''
run:

(1, 2, 3)
(1, 2, 4)
(1, 3, 4)
(2, 3, 4)

'''

 



answered Dec 8, 2019 by avibootz
0 votes
from itertools import combinations 
  
X = 3
lst = [1, 2, 3, 4]
comb = combinations(lst, X) 

for ol in list(comb): 
    for n in ol:
       print(n, end=" ")
    print()



'''
run:

1 2 3
1 2 4
1 3 4
2 3 4

'''

 



answered Dec 8, 2019 by avibootz
...