# 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

## Making a Pandas DataFrame with two-dimensional listMaking a Pandas DataFrame with two-dimensional list

In this article, we will explore how to create a Pandas DataFrame using a two-dimensional

## Python | Sort a list according to the second element in sublistPython | Sort a list according to the second element in sublist

Python – Sort a list according to the second element in sublist   In this

## Python – Create and write on excel file using xlsxwriter modulePython – Create and write on excel file using xlsxwriter module

Python | Create and write on excel file using xlsxwriter module XlsxWriter is a Python module

## Django BasicsDjango Basics

Django Basics Django is a Python-based web framework which allows you to quickly create web

## Python DecoratorsPython Decorators

Decorators in Python Decorators are a very powerful and useful tool in Python since it allows

## Underscore (_) in PythonUnderscore (_) in Python

Underscore (_) in Python Single Underscore   In Interpreter: _ returns the value of last

## Python – Converting all strings in list to integersPython – Converting all strings in list to integers

Python | Converting all strings in list to integers Interconversion between data types is facilitated

## Get() method for dictionaries in PythonGet() method for dictionaries in Python

Get() method for dictionaries in Python Python get() method return the value for the given key