itertools.combinations() Module in Python to Print All Possible Combinations Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report When given an array of size n, one common problem is generating all possible combinations of r elements.For example:Input: arr[] = [1, 2, 3, 4], r = 2Output: [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]Although, there can be a recursive solution for this problem, but in this article we'll focus on solving it using Python’s itertools.combinations().What does itertools.combinations()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 returns 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 returns 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].Let's now implement a simple function that returns all combinations of size r from an array: Python from itertools import combinations def rSubset(arr, r): return list(combinations(arr, r)) 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)] Explanation:combinations(arr, r) generates an iterator of all possible r-length combinations.list() is used to convert this iterator into a list of tuples for easy printing or further use. Comment More infoAdvertise with us Next Article itertools.combinations() Module in Python to Print All Possible Combinations S Shashank Mishra Improve Article Tags : Python Python-Library Practice Tags : python Similar Reads Itertools Combinations() function - Python The combinations() function in Python, part of the itertools module, is used to generate all possible combinations of a specified length from a given iterable (like a list, string, or tuple). Unlike permutations, where the order does matter, combinations focus only on the selection of elements, mean 2 min read Python | Print all string combination from given numbers Given an integer N as input, the task is to print the all the string combination from it in lexicographical order. Examples: Input : 191 Output : aia sa Explanation: The Possible String digit are 1, 9 and 1 --> aia 19 and 1 --> sa Input : 1119 Output : aaai aas aki kai ks Approach: Get the Str 2 min read Python | Extract Combination Mapping in two lists Sometimes, while working with Python lists, we can have a problem in which we have two lists and require to find the all possible mappings possible in all combinations. This can have possible application in mathematical problems. Let's discuss certain way in which this problem can be solved. Method 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 di 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 ok 2 min read Like