Python program to get all subsets of given size of a Set
Last Updated :
21 Feb, 2025
We are given a set, and our task is to get all subsets of a given size. For example, if we have s = {1, 2, 3, 4} and need to find subsets of size k = 2, the output should be [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)].
Using itertools.combinations
We can use itertools.combinations(iterable, r) to generate all possible subsets (combinations) of size "r" from the given set without repetition. It returns an iterator of tuples where each tuple represents a unique subset.
Python
from itertools import combinations
s = {1, 2, 3, 4}
k = 2
# Get all subsets of size k
subsets = list(combinations(s, k))
print("Subsets of size", k, ":", subsets)
OutputSubsets of size 2 : [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
Explanation:
- Combinations(s, k) function generates all possible 2-element subsets from the set {1, 2, 3, 4}, returning an iterator of tuples.
- List(combinations(s, k)) converts the iterator to a list and the result is printed, displaying all subsets of size 2.
Using bit manipulation
Using bit manipulation we can represent each subset of a set as a binary number where each bit indicates whether an element is included. By iterating through all binary numbers of length equal to set's size we can filter subsets of the desired size.
Python
s = {1, 2, 3, 4}
k = 2
s = sorted(s) # Ensure consistent ordering
n = len(s)
res = []
for i in range(1 << n): # Iterate through all possible combinations
subset = [s[j] for j in range(n) if (i & (1 << j))]
if len(subset) == k:
res.append(subset)
print("Subsets of size", k, ":", res)
OutputSubsets of size 2 : [[1, 2], [1, 3], [2, 3], [1, 4], [2, 4], [3, 4]]
Explanation:
- We iterate through all possible binary numbers of length equal to the size of the set (1 << n gives 2^n possibilities), each bit in the binary representation of i determines whether the corresponding element is included.
- The expression (i & (1 << j)) checks if the j-th bit is set, meaning the element should be part of the subset.
- We collect subsets of size k and append them to the result list and Sorting s ensures a consistent lexicographic order of subsets.
Using list comprehensions
List comprehensions can be used to efficiently filter and collect subsets of a given size by iterating over all possible combinations. They offer a concise readable way to generate subsets without explicitly using loops or conditionals.
Python
s = {1, 2, 3, 4}
k = 2
s = list(s)
n = len(s)
res = [[]]
# Iteratively build subsets
for ele in s:
ns = [s + [ele] for s in res]
res.extend(ns)
# Filter subsets of size `k`
sub = [s for s in res if len(s) == k]
print("Subsets of size", k, ":", sub)
OutputSubsets of size 2 : [[1, 2], [1, 3], [2, 3], [1, 4], [2, 4], [3, 4]]
Explanation:
- Loop iterates over each element creating new subsets by appending element to all existing subsets (s + [ele]), effectively generating all possible subsets.
- list comprehension [s for s in res if len(s) == k] is used to filter and collect only those subsets with size k
Similar Reads
Python program to get all subsets having sum x We are given a list of n numbers and a number x, the task is to write a python program to find out all possible subsets of the list such that their sum is x. Examples: Input: arr = [2, 4, 5, 9], x = 15 Output: [2, 4, 9] 15 can be obtained by adding 2, 4 and 9 from the given list. Input : arr = [10,
3 min read
Python Program to Find Duplicate sets in list of sets Given a list of sets, the task is to write a Python program to find duplicate sets. Input : test_list = [{4, 5, 6, 1}, {6, 4, 1, 5}, {1, 3, 4, 3}, {1, 4, 3}, {7, 8, 9}]Output : [frozenset({1, 4, 5, 6}), frozenset({1, 3, 4})]Explanation : {1, 4, 5, 6} is similar to {6, 4, 1, 5} hence part of result.
8 min read
Python program to convert Set into Tuple and Tuple into Set Let's see how to convert the set into tuple and tuple into the set. For performing the task we are use some methods like tuple(), set(), type(). tuple(): tuple method is used to convert into a tuple. This method accepts other type values as an argument and returns a tuple type value.set(): set metho
7 min read
How To Create A Set Of Sets In Python? Sets are flexible data structures in Python that hold distinct items. There are situations in which you may need to construct a set of sets, even though sets are unordered and flexible in and of themselves. In this article, we will see how to create a set of sets. Note: We can't create a set of sets
2 min read
Python Program to get K length groups with given summation Given a list, our task is to write a Python program to extract all K length sublists with lead to given summation. Input : test_list = [6, 3, 12, 7, 4, 11], N = 21, K = 4 Output : [(6, 6, 6, 3), (6, 6, 3, 6), (6, 3, 6, 6), (6, 7, 4, 4), (6, 4, 7, 4), (6, 4, 4, 7), (3, 6, 6, 6), (3, 3, 3, 12), (3, 3,
6 min read