Python Forum

Full Version: which itertools method
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i have been looking for a method that can generate (an iterator) all possible combinations of a given sequence in any order. for example for AB i want to get AA AB BA and BB. for ABC i want to get AAA AAB AAC ABA ABB ABC ACA ACB ACC BAA BAB BAC BBA BBB BBC BCA BCB BCC CAA CAB CAC CBA CBB CBC CCA CCB CCC. which function or method can do that?
import itertools

result = (itertools.product('AB', repeat=2))
result = map(''.join, result)
print(list(result))

result = (itertools.product('ABC', repeat=3))
result = map(''.join, result)
print(list(result))
Output:
['AA', 'AB', 'BA', 'BB'] ['AAA', 'AAB', 'AAC', 'ABA', 'ABB', 'ABC', 'ACA', 'ACB', 'ACC', 'BAA', 'BAB', 'BAC', 'BBA', 'BBB', 'BBC', 'BCA', 'BCB', 'BCC', 'CAA', 'CAB', 'CAC', 'CBA', 'CBB', 'CBC', 'CCA', 'CCB', 'CCC']
it's the repeat= keyword argument i didn't have when i tried itertools.product().