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 themselves or in combination to form iterator algebra.
Printing Combinations Using itertools
Using Itertools we can display all the possible combinations of the string in a quite optimized way. To display the combination, it requires 2 parameters. First is the string and second is the length of substrings needed. The following example makes all combinations for the string ‘abc’ using itertools.
Example:
# Import combinations from itertools from itertools import combinations def n_length_combo(arr, n): # using set to deal # with duplicates return list(combinations(arr, n)) # Driver Function if __name__ == "__main__": arr = 'abc' n = 2 print (n_length_combo([x for x in arr], n) ) |
Output
[('a', 'b'), ('a', 'c'), ('b', 'c')]
Printing Combinations Without using itertools
- By using recursion.
To create combinations without using itertools, iterate the list one by one and fix the first element of the list and make combinations with the remaining list. Similarly, iterate with all the list elements one by one by recursion of the remaining list.
# Function to create combinations# without itertoolsdefn_length_combo(lst, n):ifn==0:return[[]]l=[]foriinrange(0,len(lst)):m=lst[i]remLst=lst[i+1:]forpinn_length_combo(remLst, n-1):l.append([m]+p)returnl# Driver codeif__name__=="__main__":arr="abc"print(n_length_combo([xforxinarr],2))chevron_rightfilter_noneOutput
[('a', 'b'), ('a', 'c'), ('b', 'c')] - By using iterations
In this, return the first combination of n elements from the string as it is, then other combinations are made by considering each element by its position. Each element is treated as unique based on its position, not on its value. So if the input elements are unique, there will be no repeat values in each combination.
importnumpydefn_length_combo(iterable, r):char=tuple(iterable)n=len(char)ifr > n:returnindex=numpy.arange(r)# retruns the first sequenceyieldtuple(char[i]foriinindex)whileTrue:foriinreversed(range(r)):ifindex[i] !=i+n-r:breakelse:returnindex[i]+=1forjinrange(i+1, r):index[j]=index[j-1]+1yieldtuple(char[i]foriinindex)# Driver codeprint([xforxinn_length_combo("abc",2)])chevron_rightfilter_noneOutput
[('a', 'b'), ('a', 'c'), ('b', 'c')]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- itertools.combinations() module in Python to print all possible combinations
- Python - Itertools Combinations() function
- Print first n distinct permutations of string using itertools in Python
- Python - Itertools.tee()
- Python Itertools
- itertools.groupby() in Python
- Python - itertools.repeat()
- Python - Itertools.takewhile()
- Python - Itertools.compress()
- Python - Itertools.dropwhile()
- Python - Itertools.cycle()
- Python - Itertools.chain()
- Python - Itertools.starmap()
- Python - Itertools.Permutations()
- Python program to apply itertools.product to elements of a list of lists
- Python - Itertools.zip_longest()
- Python - Itertools.islice()
- Python - Itertools.Product()
- Python - Itertools.accumulate()
- Python - Itertools.count()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

