I have a written the code here but it is taking much time. I need to optimize it more. The k here varies, it can be 2 or 3 or 4, depends on the user. Please help me out here.
from itertools import combinations
def get_all_combinations(input_list, k):
for item in combinations(input_list, k):
yield list(set(list(item)))
input_list = [1, 2, 1, 2, 3]
k = 3
lst = []
for i in range(1, k + 1):
for item in get_all_combinations(input_list, i):
if len(item) > i - 1:
lst.append(item)
print(len(lst))
>>17
#[[1], [2], [1], [2], [3], [1, 2], [1, 2], [1, 3], [1, 2], [2, 3], [1, 2], [1, 3], [2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]