Python to list all the combinations of any-3-elements in a list

Question:

Finding a way to list all the combinations of any-3-elements in a list:

Here is what I tried:

import itertools

the_list = ["Alpha","Beta","Gamma","Delta","Epsilon","Zeta"]


list_of_trios = [(the_list[p1], the_list[p2], the_list[p3]) for p1 in range(len(the_list)) for p2 in range(p1+1,len(the_list)) for p3 in range(p1+2,len(the_list))]
tem_list = []


for each in list_of_trios:
    check_me = list(set(each))
    if len(check_me) == 3:
        tem_list.append(check_me)

tem_list.sort()

final_list = list(tem_list for tem_list, _ in itertools.groupby(tem_list))

for ox in final_list:
    print (ox)

It seems work. What would be the better way to achieve such? Thank you.

Asked By: Mark K

||

Answers:

https://docs.python.org/3/library/itertools.html?highlight=comb#itertools.combinations

from itertools import combinations

the_list = ["Alpha","Beta","Gamma","Delta","Epsilon","Zeta"]

list_of_trios = list(combinations(the_list, 3))
Answered By: Raibek
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.