×

Python Combination

Python Combination

Permutations and combinations are the important part of our mathematical tools. We use them often in our Python programs. In this tutorial, we will learn about combinations in Python and how we can use them in our Python program.

Python Combination

In this tutorial, we will learn that how we can calculate combinations from a given data with the help of Python program. For this, we are going to use the inbuilt packages to find the combinations of the given data in the program.

In Python, we have libraries such as itertools which have the in-built features to calculate the combinations of the data or number.

Importing the pre required library (itertools library):

To calculate the combinations of a number or data with the help of our Python program, we have to firstly import the pre required library i.e., itertools library in the program. To import this library in our Python program, we have to use the following command:

> - import itertools

The above command will import the itertools library and all the inbuilt features in the program. It will also form a pathway through which we can use all the functions of itertools in the Python program.

We have to remember the following points before importing the itertools library and using its functions in the Python program:

  • Firstly, we have to create a list of the sequence of the given data as the input of the program.
  • After that, the list we have created will return the output of the program in the form of Python tuples that consists of required number of permutations and combinations.
  • We can also check and set the length of permutations and combinations required in the output of the program.

Calculating Python Combinations of Different Data

As we all know that, combination is the set of data created with the help of given data where a specific order doesn't matter. In Python itertools library, we have provided with a combination() function. With the help of this combination() function, we can calculate all the different possible combinations of the given data.

In the Python program, we will use the different itertools functions to calculate the different combinations of data provided to us. Following are the different ways or method by which we can calculate the combinations of any data provided to us:

1. Combinations of a given string

2. Combinations with the help of replacement

3. Combinations of a given numeric set

Let's learn about each method with the help of some Python programs and understand how we can use them to calculate combinations of different type of data provided to us.

1. Combinations of a given string:

In Python, with the help of combination() function provided to us, we can calculate the different combinations of a string type data provided to us as input. Let's look at some examples of Python program and understand this in a better way.

Example 1:

 # importing the itertools library
 import itertools
 # define a sequence
 sequen = "PythonDeveleopers"
 # calculate possible combinations with letter 2
 combi = itertools.combinations(sequen, 2)
 # printing all combinations in output using for loop
 for m in combi: 
     print(m) 

Output of the program:

 ('P', 'y')
 ('P', 't')
 ('P', 'h')
 ('P', 'o')
 ('P', 'n')
 ('P', 'D')
 ('P', 'e')
 ('P', 'v')
 ('P', 'e')
 ('P', 'l')
 ('P', 'e')
 ('P', 'o')
 ('P', 'p')
 ('P', 'e')
 ('P', 'r')
 ('P', 's')
 ('y', 't')
 ('y', 'h')
 ('y', 'o')
 ('y', 'n')
 ('y', 'D')
 ('y', 'e')
 ('y', 'v')
 ('y', 'e')
 ('y', 'l')
 ('y', 'e')
 ('y', 'o')
 ('y', 'p')
 ('y', 'e')
 ('y', 'r')
 ('y', 's')
 ('t', 'h')
 ('t', 'o')
 ('h', 'r')
 ('h', 's')
 ('o', 'n')
 ('o', 'D')
 ('o', 'e')
 ('o', 'v')
 ('o', 'e') …. Continued …. 

Example 2::

 # importing the itertools library
 import itertools
 # define a sequence
 sequen = "TUTORIALANDEXAMPLE"
 # calculate possible combinations with letter 2
 combi = itertools.combinations(sequen, 2)
 # printing all combinations in output using for loop
 for m in combi: 
     print( m) 

Output of the program:

 ('A', 'P')
 ('A', 'L')
 ('A', 'E')
 ('N', 'D')
 ('N', 'E')
 ('N', 'X')
 ('N', 'A')
 ('N', 'M')
 ('N', 'P')
 ('N', 'L')
 ('N', 'E')
 ('D', 'E')
 ('D', 'X')
 ('D', 'A')
 ('D', 'M')
 ('D', 'P')
 ('D', 'L')
 ('D', 'E')
 ('E', 'X')
 ('E', 'A')
 ('E', 'M')
 ('E', 'P')
 ('E', 'L')
 ('E', 'E')
 ('X', 'A')
 ('X', 'M')
 ('X', 'P')
 ('X', 'L')
 ('X', 'E')
 ('A', 'M')
 ('A', 'P')
 ('A', 'L')
 ('A', 'E')
 ('M', 'P')
 ('M', 'L')
 ('M', 'E')
 ('P', 'L')
 ('P', 'E')
 ('L', 'E') …. Continued …. 

Explanation: In the above programs, we have firstly imported the itertools library. Then, we have defined a sequence in both programs. After that, we have calculated a particular set of combination with length 4 and 5 in example 1 & 2 respectively. We calculated the combinations using the itertools.combinations() function. After that, we have used a for loop in each example to print the calculated number of combinations in the output of the program. As we can see in the output, we have printed all different combinations.

2. Combinations with the help of replacement:

In itertools library, there is one more function present which we can use to find the all possible combinations of given input data. This method us called as combination_with_replacement(). It is very useful as it takes into consideration about the combination of a number itself while calculating all possible number of combinations in the given data. Let's look at some examples of Python program and understand this combination_with_replacement() method in a better way.

Example 1:

 # importing the itertools library
 import itertools
 # define a sequence
 sequen = ['T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'A', 'N', 'D', 'E', 'X', 'A', 'M', 'P', 'L', 'E']
 # calculate possible combinations with letter 2
 combi = itertools.combinations_with_replacement(sequen, 2)
 # printing all combinations in output using for loop
 for m in combi: 
     print(m) 

Output of the program:

 ('T', 'T')
 ('T', 'U')
 ('T', 'T')
 ('T', 'O')
 ('T', 'R')
 ('T', 'I')
 ('T', 'A')
 ('T', 'L')
 ('T', 'A')
 ('T', 'N')
 ('T', 'D')
 ('T', 'E')
 ('T', 'X')
 ('T', 'A')
 ('T', 'M')
 ('T', 'P')
 ('T', 'L')
 ('T', 'E')
 ('U', 'U')
 ('U', 'T')
 ('U', 'O')
 ('U', 'R')
 ('U', 'I')
 ('U', 'A')
 ('U', 'L')
 ('U', 'A')
 ('U', 'N')
 ('U', 'D')
 ('U', 'E')
 ('U', 'X')
 ('U', 'A')
 ('U', 'M')
 ('U', 'P')
 ('U', 'L')
 ('U', 'E')
 ('T', 'T')
 ('T', 'O')
 ('T', 'R')
 ('T', 'I')
 ('T', 'A')
 ('T', 'L')
 ('T', 'A')
 ('T', 'N')
 ('T', 'D')
 ('T', 'E')
 ('T', 'X')
 ('T', 'A')
 ('T', 'M')
 ('T', 'P')
 ('T', 'L')
 ('T', 'E')
 ('O', 'O')
 ('O', 'R')
 ('O', 'I')
 ('O', 'A')
 ('O', 'L')
 ('O', 'A')
 ('O', 'N')
 ('O', 'D')
 ('O', 'E')
 ('O', 'X')
 ('O', 'A')
 ('O', 'M')
 ('O', 'P')
 ('O', 'L')
 ('O', 'E')
 ('R', 'R')
 ('R', 'I')
 ('R', 'A')
 ('R', 'L')
 ('R', 'A')
 ('R', 'N')
 ('R', 'D') …. Continued … 

Explanation: In the above given program, we have used combination_with_replacement() to find all the possible combinations and then, we used the for loop to print all these combinations in the output of the program. The Python printed all these combinations in the form of tuple in the output.

3. Combinations of a given numeric set:

If the input data is in the form of numeric set and in the sorted order, the combinations_with_replacement() function of itertools library will return the output in the form of tuples. These Python tuples will be the all-possible combinations of the numeric data provided to us. We can set the length of the combination that we want in the output. Let's look at some examples of Python program and understand this in a better way.

Example 1:

 # importing the itertools library
 import itertools
 # define a sequence of numbers
 sequen = [1, 3, 5, 7, 9, 11]
 # calculate possible combinations with letter 2
 combi = itertools.combinations_with_replacement(sequen, 2)
 # printing all combinations in output using for loop
 for m in combi: 
     print(m) 

Output of the program:

 (1, 1)
 (1, 3)
 (1, 5)
 (1, 7)
 (1, 9)
 (1, 11)
 (3, 3)
 (3, 5)
 (3, 7)
 (3, 9)
 (3, 11)
 (5, 5)
 (5, 7)
 (5, 9)
 (5, 11)
 (7, 7)
 (7, 9)
 (7, 11)
 (9, 9)
 (9, 11)
 (11, 11) 

Example 2:

 # importing the itertools library
 import itertools
 # define a sequence of numbers
 sequen = [2, 4, 6, 8, 10, 12, 14] 
 # calculate possible combinations with letter 3
 combi = itertools.combinations_with_replacement(sequen, 3)
 # printing all combinations in output using for loop
 for m in combi: 
     print(m) 

Output of the program:

 (2, 2, 2)
 (2, 2, 4)
 (2, 2, 6)
 (2, 2, 8)
 (2, 2, 10)
 (2, 2, 12)
 (2, 2, 14)
 (2, 4, 4)
 (2, 4, 6)
 (2, 4, 8)
 (2, 4, 10)
 (2, 4, 12)
 (2, 4, 14)
 (2, 6, 6)
 (2, 6, 8)
 (2, 6, 10)
 (2, 6, 12)
 (2, 6, 14)
 (2, 8, 8)
 (2, 8, 10)
 (2, 8, 12)
 (2, 8, 14)
 (2, 10, 10)
 (2, 10, 12)
 (2, 10, 14)
 (2, 12, 12)
 (2, 12, 14)
 (2, 14, 14)
 (4, 4, 4)
 (4, 4, 6)
 (4, 4, 8)
 (4, 4, 10)
 (4, 4, 12)
 (4, 4, 14)
 (4, 6, 6)
 (4, 6, 8)
 (4, 6, 12) … continued … 

Explanation: In the above examples, we have provided with two different sets of numeric data. To find all the possible combinations of the given numeric data, we have imported itertools library in the program and then used the combination function on the numeric data provided to us. We used for loop to print the all-possible combinations in the output of the program. The all-possible combinations of the numeric data will be printed in the form of Python tuples.


Related Topics

Python - Binomial Distribution

Introduction Definition of the Binomial Distribution The method of counting how many instances of a specific event there have been is called the binomial distribution. It will outline the possible outcomes or...

4 minutes read.

How to create a login page in python

Users can access an application by providing their username and Password or by authenticating with a social media login on the login screen. Python Tkinter Python provides a variety of choices for...

3 minutes read.

Python maketrans() function

Introduction A static method is the string maketrans() one. It is used to make a one-to-one mapping between a string's character and its translation, i.e., to define the list of characters...

5 minutes read.

Python locals() function

Python locals() function The locals() function in Python updates and returns a dictionary representing the current local symbol table. Syntax locals() Parameter NA Return This function returns the local symbol table as a dictionary or returns free...

1 minute read.

How to Read html page in python

Html is a Hyper Text Markup language is a standard language used for creating webpages. HTML is the name of the language used to describe the construction of Web pages....

4 minutes read.

Convert Float to Int in Python using Pandas

Introduction To play with huge amounts of data, in python we require a tool. The tool which is available in Python is pandas. A panda is an open-source library. It is...

4 minutes read.

GUI to extract lyrics from a song Using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

4 minutes read.

Index Error in Python

The index errors are the run time error that is raised in Python when we try to access an index that does not exist. This might seem very trivial but...

4 minutes read.

How to print in same line in python

How to Print in the Same Line in Python To print in Python, we use the print () function and the syntax of print () goes like this: print (values, sep =...

3 minutes read.

File Explorer using Tkinter in Python

File Explorer: Users can control folders and files on a device using an application known as a file manager or file explorer. Customers can access, edit, copy, delete, and start moving files...

3 minutes read.

Magento 2 Site optimization

Magento 2 Site optimization Magento 2 is a CMS, commonly referred to as Intensive efficiency. Improving the speed and reliability of your Magento 2 app helps clients to achieve a better user experience while...

2 minutes read.

An Introduction to Mocking in Python

Python Mocking is a testing library. It enables you to substitute portions of your system under test with fake(mock) objects and make assertions about how they were utilized.  The test is...

3 minutes read.

Yield vs Return in Python

In this article, you will learn about " Yield " and " Return " in Python. You will also understand the difference between " Yield " and " Return "...

6 minutes read.

Python Keywords

If you are trying to learn a programming language, you need to have a basic idea of "What are keywords" and "How they are used". You can learn about keywords in...

7 minutes read.

Python Set remove() method

Python Set remove() method The set.remove() method in Python removes the specified element from the set if it is present in the set else this method will raise an error if the specified item does...

2 minutes read.

Python tokens and character set

In this tutorial, we will understand what are character sets used in python and what is meant by python tokens and we will further discuss the type of tokens being...

4 minutes read.

Add in Dictionary Python

Dictionary in python: Dictionaries are a useful data configuration for storing information in Python because they can mimic real-world data arrangements where a particular value exists for a particular key. The...

4 minutes read.

Python Modules List

Python is like an ocean. If you have been working with Python, you might’ve already heard the word module. When we solve a problem in mathematics, there will be several...

3 minutes read.

Python Loop through a Dictionary

Introduction in this tutorial, we will discuss in python How to Loop Through a Dictionary. In contrast to other Data Types, which can only retain a single value as an element, a Dictionary...

4 minutes read.

List Index out of Range Python for Loop

The List is generally used in Python to store multiple items or elements inside one single variable. The List is ordered in nature, the List can contain the elements inside...

3 minutes read.