The Wayback Machine - https://web.archive.org/web/20240827234648/https://www.geeksforgeeks.org/python-itertools-combinations-function/
Open In App

Python – Itertools Combinations() function

Last Updated : 24 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:-

Note: For more information, refer to Python Itertools

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 their 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:- 

Python3




# 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:- 

Python3




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,)]


Similar Reads

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 = 2Output : [(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 th
2 min read
Combinations in Python without using itertools
Itertools in Python is a module that produces complex iterators with the help of methods that work on iterators. This module works as a fast, memory-efficient tool that is used either by itself or in combination to form iterator algebra. Printing Combinations Using itertoolsUsing Itertools we can display all the possible combinations of the string
3 min read
Print first n distinct permutations of string using itertools in Python
Given a string with duplicate characters allowed, print first n permutations of given string such that no permutation is repeated. Examples: Input : string = "abcab", n = 10 Output : aabbc aabcb aacbb ababc abacb abbac abbca abcab abcba acabb Input : string = "okok", n = 4 Output : kkoo koko kook okko Approach: Python provides an inbuilt method to
2 min read
Python Itertools
Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. For example, let's suppose there are two lists and you want to multiply their elements. There can be sever
13 min read
itertools.groupby() in Python
Prerequisites: Python Itertools Python’s Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Itertools.groupby() This method calculates the keys for each element prese
2 min read
Python - itertools.repeat()
Python’s Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools repeat() itertools.repeat()falls under the category
2 min read
Python - Itertools.takewhile()
The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easy. One such itertools function is takewhile(). Note: For more information, refer to Python Itertools takewhile() This allows considering an item from the iterable until t
3 min read
Python - Itertools.compress()
Python’s Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Compress() The itertools.compress() falls under the
2 min read
Python - Itertools.dropwhile()
Itertools is a Python module that provide various functions that work on iterators to produce complex iterators. It makes the code faster, memory efficient and thus we see a better performance. This module is either used by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Dropwhile() The d
1 min read
Python - Itertools.cycle()
Iterator is defined as object types which contains values that can be accessed or iterated using a loop. There are different iterators that come built-in with Python such as lists, sets, etc. Itertools is the Python module that contains some inbuilt functions for generating sequences using iterators. This module provides various functions that work
3 min read
Python - Itertools.chain()
The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easily. One such itertools function is chain().Note: For more information, refer to Python Itertools chain() function It is a function that takes a series of iterables and r
4 min read
Python - Itertools.starmap()
The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easily. One such itertools function is starmap().Note: For more information, refer to Python Itertools starmap() function When an iterable is contained within another iterab
3 min read
Python - Itertools.Permutations()
Itertool is a module provided by Python for creating iterators for efficient looping. It also provides various features or functions that work with iterators to produce complex iterators and help us to solve problems easily and efficiently in terms of time as well as memory. Itertools module provides us various ways to manipulate the sequence that
3 min read
Python program to apply itertools.product to elements of a list of lists
Itertools is a module that consists of the methods to apply various iteration based operations including combinations, permutations, etc., on the iterable components in Python. It has a set lightweight, memory-efficient and fast tools for performing iterator algebra. Note: For more information, refer to Python Itertools itertools.product() It is us
2 min read
Python - Itertools.zip_longest()
Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Iterators in Python is an object that can iterate like sequence data types such as list, tuple, str and so
2 min read
Python - Itertools.islice()
In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is islice(). Note: For more information, refer to Python Itertools islice() function This iterator selectively prints the values mentioned in
2 min read
Python - Itertools.accumulate()
Python itertools module is a collection of tools for handling iterators. According to the official documentation: "Module [that] implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SMLâ€Ķ Together, they form an ‘iterator algebra’ making it possible to construct specialized tools succinctly and efficiently in
4 min read
Python - Itertools.count()
Python Itertools are a great way of creating complex iterators which helps in getting faster execution time and writing memory-efficient code. Itertools provide us with functions for creating infinite sequences and itertools.count() is one such function and it does exactly what it sounds like, it counts! Note: For more information, refer to Python
3 min read
Python - Itertools.Combinations_with_replacement()
Itertools in Python refers to module provided in Python for the creation of iterators which helps in efficient looping, time and space efficiency as well. Itertools helps us to solve complex problems easily and efficiently. There are in general 3 types of iterators.Different types of iterators provided by this module are: Combinatoric Generators In
2 min read
Python - Itertools.chain.from_iterable()
Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools The functions under itertools can be classified into
2 min read
Python - Itertools.filterfalse()
In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is filterfalse(). Note: For more information, refer to Python Itertools filterfalse() function This iterator prints only values that return f
2 min read
Python - Itertools.tee()
In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is tee(). Note: For more information, refer to Python Itertools tee() function This iterator splits the container into a number of iterators
2 min read
Python - Itertools.Product()
In the terms of Mathematics Cartesian Product of two sets is defined as the set of all ordered pairs (a, b) where a belongs to A and b belongs to B. Consider the below example for better understanding.Examples: Input : arr1 = [1, 2, 3] arr2 = [5, 6, 7] Output : [(1, 5), (1, 6), (1, 7), (2, 5), (2, 6), (2, 7), (3, 5), (3, 6), (3, 7)] Input : arr1 =
2 min read
Itertools in Python3
Itertools is a module in Python, it is used to iterate over data structures that can be stepped over using a for-loop. Such data structures are also known as iterables. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Why to use ? This module incorporates functions that
3 min read
Python | Size Range Combinations in list
The problem of finding the combinations of list elements of specific size has been discussed. But sometimes, we require more and we wish to have all the combinations of elements of all sizes in range between i and j. Let’s discuss certain ways in which this function can be performed. Method #1 : Using list comprehension + combinations() This task c
4 min read
Python - Nested dictionary Combinations
Sometimes, while working with Python dictionaries, we can have a problem in which we need to construct all the combination of dictionary keys with different values. This problem can have application in domains such as gaming and day-day programming. Lets discuss certain way in which we can perform this task. Input : test_dict = {'gfg': {'is' : [6],
3 min read
How to build an array of all combinations of two NumPy arrays?
Sometimes we need to find the combination of elements of two or more arrays. Numpy has a function to compute the combination of 2 or more Numpy arrays named as "numpy.meshgrid()". This function is used to create a rectangular grid out of two given one-dimensional arrays representing the Cartesian indexing or Matrix indexing. Syntax: numpy.meshgrid(
2 min read
All Combinations For A List Of Objects
Prerequisite: Python Itertools There are several ways to get all combinations for a list of objects in python. This problem has already a recursive solution. Python has an itertools module that provides two functions named combinations() and combinations_with_replacement() which make our work a lot easier. Below are the two methods: 1. Using iterto
3 min read
Pandas - All combinations of two columns
In this article, we will see how to get the combination of two columns of a DataFrame. First, let's create a sample DataFrame. Code: An example code to create a data frame using dictionary. C/C++ Code # importing pandas module for the # data frame import pandas as pd # creating data frame for student details # using dictionary data = pd.DataFrame({
1 min read
Wand function() function in Python
function() function is similar to evaluate function. In function() function pixel channels can be manipulated by applies a multi-argument function to pixel channels. Following are the list of FUNCTION_TYPES in Wand: 'undefined''arcsin''arctan''polynomial''sinusoid' Syntax : wand.image.function(function, arguments, channel) Parameters : ParameterInp
1 min read
Article Tags :
Practice Tags :