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

1 Answer

0 votes
from itertools import combinations

lst = [2, 7, 9, 5]
N = 3

comb = list(combinations(lst, N))

print(comb)

 
 
 
 
'''
run:
 
[(2, 7, 9), (2, 7, 5), (2, 9, 5), (7, 9, 5)]

'''

 



answered Sep 21, 2022 by avibootz
...