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

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
- Python | Pandas Series.size
- Python | os.path.size() method
- Python | Pandas Index.size
- Python | Pandas TimedeltaIndex.size
- Python | Pandas Panel.size
- Find size of a list in Python
- Break a list into chunks of size N in Python
- Python | Window size Adjustment in Kivy
- Python | Categorize the given list by string size
- Python | Subgroups of i'th index size in list
- Python | Pandas df.size, df.shape and df.ndim

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.