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 = 2Output :[{1, 2}, {1, 3}, {2, 3}]Input :{1, 2, 3, 4}, n = 3Output :[{1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}]

We have already discussed the same problem using Naive approach in this article. This article focuses on the Pythonic approaches to Print all subsets of 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 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.

`# 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)) ` |

*chevron_right*

*filter_none*

**Output:**

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

**Code #2 : **

We can also use an alternative to above discussed method which is *mapping *set to `itertools.combinations()`

function.

`# 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)) ` |

*chevron_right*

*filter_none*

**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.

`# 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)) ` |

*chevron_right*

*filter_none*

**Output:**

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

## Recommended Posts:

- Print all subsets of given size of a set
- Python Program for Median of two sorted arrays of same size
- Python Program for Maximum size square sub-matrix with all 1s
- Python | Minimum number of subsets with distinct elements using Counter
- Find size of a list in Python
- Python | Pandas Panel.size
- Python | Pandas Series.size
- Python | Pandas TimedeltaIndex.size
- Break a list into chunks of size N in Python
- Python | Pandas df.size, df.shape and df.ndim
- Python Slicing | Reverse an array in groups of given size
- Filtering Images based on size attributes in Python
- Python Counter to find the size of largest subset of anagram words
- Python Program for Efficient program to print all prime factors of a given number
- Python Program for Program to find area of a circle