Is there a way to quickly select 3 items of a list?

I am trying to create a list of subsets of n of length 3. However, my approaches are quite slow, and it takes a while to run the whole program.

My “code” so far is just the generic iterative approach.

subs = []
for a in vertices:
    for b in vertices:
        for c in vertices:
            if sorted([a, b, c]) not in subs and a != b and b != c:
                subs.append(sorted([a, b, c]))

This is quite slow, but I’m not sure exactly how I could speed it up. Could anyone help?



import itertools
xs = [1, 2, 3,4]
print(list(itertools.combinations(xs, 3)))
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]

Leave a Reply

Your email address will not be published. Required fields are marked *