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}]

Attention geek! Strengthen your foundations with the **Python Programming Foundation** Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the **Python DS** Course.

## Recommended Posts:

- Sum of subsets of all the subsets of an array | O(N)
- Print all subsets of given size of a set
- Product of all sorted subsets of size K using elements whose index divide K completely
- Find all distinct subsets of a given set
- Total number of Subsets of size at most K
- Python | Find the number of unique subsets with given sum in array
- How to get file size in Python?
- Sum of product of all subsets formed by only divisors of N
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Maximum sum of Bitwise XOR of all elements of two equal length subsets
- Python Program for Maximum size square sub-matrix with all 1s
- Python program to get all unique combinations of two Lists
- Number of subsets with sum divisible by M | Set 2
- PyQt5 - How to adjust size of ComboBox according to the items size
- PyQt5 – Get the size of push button
- PyQt5 - Get the icon size of item icon in ComboBox
- PyQt5 Combo Box - Different border size when in OFF state when get pressed
- PyQt5 Combo Box – Different border size when it is editable and get pressed
- PyQt5 ComboBox – Different border size when non-editable and get pressed
- PyQt5 QSpinBox - How to get the size hint

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.