# Python – Itertools Combinations() function

## Python – Itertools Combinations() function

Itertool is a module of Python which is used to creation of iterators which helps us in efficient looping in terms of space as well as time. This module helps us to solve complex problems easily with the help of different sub-functions of itertools. The different sub-functions are divided into 3 subgroups which are:-

• Infinite Iterators
• Iterators terminating on the shortest input sequence
• Combinatoric Generators

## Itertools.combinations()

`Itertools.combinations()` falls under the third subcategory called “Combinatoric Generators”. Combinatoric Generators are those iterators that are used to simplify combinatorial constructs such as permutations, combinations, and Cartesian products

As understood by name combinations is refers to a sequence or set of numbers or letters used in the iterator. Similarly `itertools.combinations()` provides us with all the possible tuples a sequence or set of numbers or letters used in the iterator and the elements are assumed to be unique on the basis of there positions which are distinct for all elements. All these combinations are emitted in lexicographical order. This function takes ‘r’ as input here ‘r’ represents the size of different combinations that are possible. All the combinations emitted are of length ‘r’ and ‘r’ is a necessary argument here.

Syntax:

`combinations(iterator, r)`

Example 1:-

 `# Combinations Of string "GeEKS" OF SIZE 3.` ` ` ` ` `from` `itertools ``import` `combinations` ` ` `letters ``=``"GeEKS"` ` ` `# size of combination is set to 3` `a ``=` `combinations(letters, ``3``) ` `y ``=` `[``' '``.join(i) ``for` `i ``in` `a]` ` ` `print``(y)`

Output:-

```['G e E', 'G e K', 'G e S', 'G E K', 'G E S', 'G K S', 'e E K', 'e E S', 'e K S', 'E K S']
```

Example 2:-

 `from` `itertools ``import` `combinations` ` ` `     ` `print` `(``"All the combination of list in sorted order(without replacement) is:"``)   ` `print``(``list``(combinations([``'A'``, ``2``], ``2``)))  ` `print``()  ` `     ` `print` `(``"All the combination of string in sorted order(without replacement) is:"``)  ` `print``(``list``(combinations(``'AB'``, ``2``)))  ` `print``()  ` `     ` `print` `(``"All the combination of list in sorted order(without replacement) is:"``)  ` `print``(``list``(combinations(``range``(``2``), ``1``))) `

Output :-

```All the combination of list in sorted order(without replacement) is:
[('A', 2)]

All the combination of string in sorted order(without replacement) is:
[('A', 'B')]

All the combination of list in sorted order(without replacement) is:
[(0,), (1,)]```

Last Updated on November 13, 2021 by admin

## How to clear screen in python?How to clear screen in python?

How to clear screen in python? Most of the time, while working with python interactive

## Python – os.path.dirname() methodPython – os.path.dirname() method

Python | os.path.dirname() method OS module in Python provides functions for interacting with the operating system.

## How to get column and row names in DataFrame?How to get column and row names in DataFrame?

Getting column and row names in DataFrame While analyzing the real datasets which are often

## Python | Pandas dataframe.round()Python | Pandas dataframe.round()

Pandas dataframe.round()   Pandas dataframe.round() function is used to round a DataFrame to a variable

## mode() function in Python statistics modulemode() function in Python statistics module

mode() function in Python statistics module The mode of a set of data values is the value

## Python Program for Sum the digits of a given numberPython Program for Sum the digits of a given number

Python Program for Sum the digits of a given number Given a number and the

## Lambda and filter in PythonLambda and filter in Python

Lambda and filter in Python Examples Given a list of numbers, find all numbers divisible