Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,926 questions

51,859 answers

573 users

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

1 Answer

0 votes
from itertools import combinations 
   
lst = list(range(1, 11))
X = 5
comb = combinations(lst, X) 
 
for ol in list(comb): 
    print(ol)
 
 
 
'''
run:
 
(2, 4, 8, 9, 10)
(2, 5, 6, 7, 8)
(2, 5, 6, 7, 9)
(2, 5, 6, 7, 10)
(2, 5, 6, 8, 9)
(2, 5, 6, 8, 10)
(2, 5, 6, 9, 10)
(2, 5, 7, 8, 9)
(2, 5, 7, 8, 10)
(2, 5, 7, 9, 10)
(2, 5, 8, 9, 10)
(2, 6, 7, 8, 9)
(2, 6, 7, 8, 10)
(2, 6, 7, 9, 10)
(2, 6, 8, 9, 10)
(2, 7, 8, 9, 10)
(3, 4, 5, 6, 7)
(3, 4, 5, 6, 8)
(3, 4, 5, 6, 9)
(3, 4, 5, 6, 10)
(3, 4, 5, 7, 8)
(3, 4, 5, 7, 9)
(3, 4, 5, 7, 10)
(3, 4, 5, 8, 9)
(3, 4, 5, 8, 10)
(3, 4, 5, 9, 10)
(3, 4, 6, 7, 8)
(3, 4, 6, 7, 9)
(3, 4, 6, 7, 10)
(3, 4, 6, 8, 9)
(3, 4, 6, 8, 10)
(3, 4, 6, 9, 10)
(3, 4, 7, 8, 9)
(3, 4, 7, 8, 10)
(3, 4, 7, 9, 10)
(3, 4, 8, 9, 10)
(3, 5, 6, 7, 8)
(3, 5, 6, 7, 9)
(3, 5, 6, 7, 10)
(3, 5, 6, 8, 9)
(3, 5, 6, 8, 10)
(3, 5, 6, 9, 10)
(3, 5, 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