 # Python to print all possible combinations – itertools.combinations() module

## itertools.combinations() module in Python to print all possible combinations

Given an array of size n, generate and print all possible combinations of r elements in array.

Examples:

```Input : arr[] = [1, 2, 3, 4],
r = 2
Output : [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]```

This problem has existing recursive solution please refer Print all possible combinations of r elements in a given array of size n link. We will solve this problem in python using itertools.combinations() module.

### What does itertools.combinations() do ?

It returns r length subsequences of elements from the input iterable. Combinations are emitted in lexicographic sort order. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.

• itertools.combinations(iterable, r) :
It return r-length tuples in sorted order with no repeated elements. For Example, combinations(‘ABCD’, 2) ==> [AB, AC, AD, BC, BD, CD].
• itertools.combinations_with_replacement(iterable, r) :
It return r-length tuples in sorted order with repeated elements. For Example, combinations_with_replacement(‘ABCD’, 2) ==> [AA, AB, AC, AD, BB, BC, BD, CC, CD, DD].
 `# Function which returns subset or r length from n` `from` `itertools ``import` `combinations` ` ` `def` `rSubset(arr, r):` ` ` `    ``# return list of all subsets of length r` `    ``# to deal with duplicate subsets use ` `    ``# set(list(combinations(arr, r)))` `    ``return` `list``(combinations(arr, r))` ` ` `# Driver Function` `if` `__name__ ``=``=` `"__main__"``:` `    ``arr ``=` `[``1``, ``2``, ``3``, ``4``]` `    ``r ``=` `2` `    ``print` `(rSubset(arr, r))`

Output:

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

Last Updated on October 28, 2021 by admin

## Box Plot in Python using MatplotlibBox Plot in Python using Matplotlib

Box Plot in Python using Matplotlib A Box Plot is also known as Whisker plot is created to display the summary of the set of data values having properties like minimum, first quartile, median, third quartile and maximum. In the box plot, a box

## Python format() functionPython format() function

In Python, the format() function is a powerful tool that simplifies string formatting. It allows you to create formatted strings by replacing placeholders with specific values. This article will guide you through the various features and techniques of using the

## Python – after method in TkinterPython – after method in Tkinter

Python | after method in Tkinter Tkinter provides a variety of built-in functions develop interactive and featured GUI (Graphical User Interface). after() function is also a Universal function which can be used directly on the root as well as with other widgets.

## Counting the frequencies in a list using dictionary in PythonCounting the frequencies in a list using dictionary in Python

Counting the frequencies in a list using dictionary in Python   Given an unsorted list of some elements(may or may not be integers), Find the frequency of each distinct element in the list using a dictionary. Example: Input : [1,

## Count Values in Pandas DataframeCount Values in Pandas Dataframe

Count Values in Pandas Dataframe   In this article, we are going to count values in Pandas dataframe. First, we will create a data frame, and then we will count the values of different attributes. Syntax: DataFrame.count(axis=0, level=None, numeric_only=False) Parameters:

## Python List count() methodPython List count() method

Python List count() method Python List count() is an inbuilt function in Python that returns the count of how many times a given object occurs in a List. The count() function is used to count elements on a list as well as a

## Pandas dataframe.drop_duplicates()Pandas dataframe.drop_duplicates()

Python – Pandas dataframe.drop_duplicates()   Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. An important

## Python – Check if two lists are identicalPython – Check if two lists are identical

Python – Check if two lists are identical   This article deals with the task of ways to check if two unordered list contains exact similar elements in exact similar position, i.e to check if two lists are exactly equal.