subset
Generate all subsets of size k (containing k elements) in Python
Seems like you want itertools.combinations: >>> list(itertools.combinations((1, 2, 3), 2)) [(1, 2), (1, 3), (2, 3)] If you want sets you’ll have to convert them explicitly. If you don’t mind an iterable instead of a list, and you’re using Python 3, you can use map: >>> s = set((1, 2, 3)) >>> map(set, itertools.combinations(s, 2)) … Read more