Open In App

Python program to get all subsets of given size of a set

Last Updated : 09 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given a set, write a Python program to generate all possible subset of size n of given set within a list.
 

Examples: 

Input : {1, 2, 3}, n = 2
Output : [{1, 2}, {1, 3}, {2, 3}]

Input : {1, 2, 3, 4}, n = 3
Output : [{1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}]

We have already discussed the same problem using the Naive approach in this article. This article focuses on the Pythonic approaches to Print all subsets of a given size of a set.

Python has itertools.combinations(iterable, n) which Return n length subsequences of elements from the input iterable. This can be used to Print all subsets of a given size of a set. Now, we have various alternatives to use this function.

Code #1 : 
Simply pass the set as iterable and the size as arguments in the itertools.combinations() to directly fetch the combination list.
 

Python3




# Python Program to Print
# all subsets of given size of a set
 
import itertools
 
def findsubsets(s, n):
    return list(itertools.combinations(s, n))
 
# Driver Code
s = {1, 2, 3}
n = 2
 
print(findsubsets(s, n))


Output: 

[(1, 2), (1, 3), (2, 3)]

 

  
Code #2 : 
We can also use an alternative to the above-discussed method which is mapping set to itertools.combinations() function. 
 

Python3




# Python Program to Print
# all subsets of given size of a set
 
import itertools
from itertools import combinations, chain
 
def findsubsets(s, n):
    return list(map(set, itertools.combinations(s, n)))
     
# Driver Code
s = {1, 2, 3}
n = 2
 
print(findsubsets(s, n))


Output: 

[{1, 2}, {1, 3}, {2, 3}]

 

  
Code #3 : 
Another method is to use for loop in itertools.combinations() function and append the combination sets to the list. 
 

Python3




# Python Program to Print
# all subsets of given size of a set
 
import itertools
# def findsubsets(s, n):
def findsubsets(s, n):
    return [set(i) for i in itertools.combinations(s, n)]
     
# Driver Code
s = {1, 2, 3, 4}
n = 3
 
print(findsubsets(s, n))


Output:

[{1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}]

Code #4:

Many a time when this question is asked in interviews, it’s better to answer without using any module. So, here is the solution that does not use itertools module:

Python3




def subsets(numbers):
    if numbers == []:
        return [[]]
    x = subsets(numbers[1:])
    return x + [[numbers[0]] + y for y in x]
 
# wrapper function
def subsets_of_given_size(numbers, n):
    return [x for x in subsets(numbers) if len(x)==n]
 
if __name__ == '__main__':
    numbers = [1, 2, 3, 4]
    n = 3
    print(subsets_of_given_size(numbers, n))


Output:

[[2, 3, 4], [1, 3, 4], [1, 2, 4], [1, 2, 3]]


Similar Reads

Maximum number of subsets an array can be split into such that product of their minimums with size of subsets is at least K
Given an array arr[] consisting of N integers and an integer K, the task is to find the maximum number of disjoint subsets that the given array can be split into such that the product of the minimum element of each subset with the size of the subset is at least K. Examples: Input: arr[] = {7, 11, 2, 9, 5}, K = 10Output: 2Explanation:All such disjoi
8 min read
Print all subsets of given size of a set
Generate all possible subsets of size r of the given array with distinct elements. Examples: Input : arr[] = {1, 2, 3, 4} r = 2Output : 1 2 1 3 1 4 2 3 2 4 3 4Input : arr[] = {10, 20, 30, 40, 50} r = 3Output : 10 20 30 10 20 40 10 20 50 10 30 40 10 30 50 10 40 50 20 30 40 20 30 50 20 40 50 30 40 50 Recommended: Please try your approach on {IDE} fir
15+ min read
Sum of subsets of all the subsets of an array | O(3^N)
Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset will be {} and {1}, Sum = 0 + 1 = 1 c) {1} : 1 All t
6 min read
Sum of subsets of all the subsets of an array | O(2^N)
Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset will be {} and {1}, Sum = 0 + 1 = 1 c) {1} : 1 All t
6 min read
Split array into minimum number of subsets such that elements of all pairs are present in different subsets at least once
Given an array arr[] consisting of N distinct integers, the task is to find the minimum number of times the array needs to be split into two subsets such that elements of each pair are present into two different subsets at least once. Examples: Input: arr[] = { 3, 4, 2, 1, 5 } Output: 3 Explanation: Possible pairs are { (1, 2), (1, 3), (1, 4), (1,
6 min read
Sum of subsets of all the subsets of an array | O(N)
Given an array arr[] of length N, the task is to find the overall sum of subsets of all the subsets of the array.Examples: Input: arr[] = {1, 1} Output: 6 All possible subsets: a) {} : 0 All the possible subsets of this subset will be {}, Sum = 0 b) {1} : 1 All the possible subsets of this subset will be {} and {1}, Sum = 0 + 1 = 1 c) {1} : 1 All t
7 min read
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, 20, 25, 50, 70, 90], x = 80 Output : [10, 70] [10,
3 min read
Sum of products of all possible K size subsets of the given array
Given an array arr[] of N non-negative integers and an integer 1 ? K ? N. The task is to find the sum of the products of all possible subsets of arr[] of size K. Examples: Input: arr[] = {1, 2, 3, 4}, K = 2 Output: 35 (1 * 2) + (1 * 3) + (1 * 4) + (2 * 3) + (2 * 4) + (3 * 4) = 2 + 3 + 4 + 6 + 8 + 12 = 35 Input: arr[] = {1, 2, 3, 4}, K = 3 Output: 5
15+ min read
Sum of all subsets of a given size (=K)
Given an array arr[] consisting of N integers and a positive integer K, the task is to find the sum of all the subsets of size K. Examples: Input: arr[] = {1, 2, 4, 5}, K = 2Output: 36Explanation:The subsets of size K(= 2) are = {1, 2}, {1, 4}, {1, 5}, {2, 4}, {2, 5}, {4, 5}. Now, the sum of all subsets sum = 3 + 5 + 6 + 6 + 7 + 9 = 36. Input: arr[
7 min read
Find maximum AND value among all K-size subsets of given Array
Given an array arr[] containing N non-negative integers, the task is to find the maximum AND value among all the subsets having length K. Examples: Input: arr[] = {1, 6, 9, 7}, K = 1Output: 9Explanation: As only one element is allowed 9 is the greatest value that can be obtained. Input: arr[] = {3, 3, 3}, K = 2Output: 3 Input: arr[] = {7, 8, 9, 10,
7 min read