Generate all possible combinations of characters, of every length, from an array using Python

Question:

I want to generate all possible combinations of letters from array ["a", "b", "c"]
I’ve seen programs that should have generated all combinations but I got [('a', 'b'), ('a', 'c'), ('b', 'c')] which is wrong because its missing ('b', 'a'), ('b', 'b') ('c', 'a'), ('c', 'b'). Does anyone have any idea for how to generate ALL of the combinations while code still being fast?

I’ve checked other questions about this but they were in different language other than Python or they just didn’t generate all possible combinations. Some did generate all of the combinations, but with numbers and with specific length. I want all of the character combinations.

Here’s code that didn’t work properly (Didn’t generate all combinations):

import itertools

stuff = [1, 2, 3]
for L in range(len(stuff) + 1):
    for subset in itertools.combinations(stuff, L):
        print(subset)

Here’s another one (Also didn’t generate all combinations):

from itertools import chain, combinations
def all_subsets(ss):
    return chain(*map(lambda x: combinations(ss, x), range(0, len(ss)+1)))

for subset in all_subsets(stuff):
    print(subset)

Both outputs are:

()
('a',)
('b',)
('c',)
('a', 'b')
('a', 'c')
('b', 'c')
('a', 'b', 'c')

Not all combinations.

Here’s what I want:

()
('a',)
('b',)
('c',)
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'b')
('b', 'c')
('c', 'a')
('c', 'b')
('c', 'c')
('a', 'b', 'c')
...

Every possible combination. As if you have a keyboard with only A B and C keys and you’re trying to write everything. If you think about it, this means that some combinations can be (‘a’, ‘a’, ‘a’, ‘a’, ‘a’, ‘a’) and so on, so lets set max length at 30 characters.

Asked By: Voil

||

Answers:

from itertools import product

text = 'abc'

all_product = sum((list(product(text,repeat=i)) for i in range(len(text)+1)),[])
print(all_product)

What you are looking for is product from the built-in packages. You need to increase the repeat parameter from 0 to the length of the input text, that’s why I added 1, so the range is from 0 to 3.

Outputs:

[(), ('a',), ('b',), ('c',), ('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'), ('c', 'a'), ('c', 'b'), ('c', 'c'), ('a', 'a', 'a'), ('a', 'a', 'b'), ('a', 'a', 'c'), ('a', 'b', 'a'), ('a', 'b', 'b'), ('a', 'b', 'c'), ('a', 'c', 'a'), ('a', 'c', 'b'), ('a', 'c', 'c'), ('b', 'a', 'a'), ('b', 'a', 'b'), ('b', 'a', 'c'), ('b', 'b', 'a'), ('b', 'b', 'b'), ('b', 'b', 'c'), ('b', 'c', 'a'), ('b', 'c', 'b'), ('b', 'c', 'c'), ('c', 'a', 'a'), ('c', 'a', 'b'), ('c', 'a', 'c'), ('c', 'b', 'a'), ('c', 'b', 'b'), ('c', 'b', 'c'), ('c', 'c', 'a'), ('c', 'c', 'b'), ('c', 'c', 'c')]
Answered By: Gábor Fekete

You can use itertools.product.

from itertools import product

stuff = [0, 1, 2, 3]

for i in stuff:
    for pro in product('abc', repeat=i):
        print(pro)

Output:

()
('a',)
('b',)
('c',)
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'b')
('b', 'c')
('c', 'a')
('c', 'b')
('c', 'c')
('a', 'a', 'a')
('a', 'a', 'b')
('a', 'a', 'c')
('a', 'b', 'a')
('a', 'b', 'b')
('a', 'b', 'c')
('a', 'c', 'a')
('a', 'c', 'b')
('a', 'c', 'c')
('b', 'a', 'a')
('b', 'a', 'b')
('b', 'a', 'c')
('b', 'b', 'a')
('b', 'b', 'b')
('b', 'b', 'c')
('b', 'c', 'a')
('b', 'c', 'b')
('b', 'c', 'c')
('c', 'a', 'a')
('c', 'a', 'b')
('c', 'a', 'c')
('c', 'b', 'a')
('c', 'b', 'b')
('c', 'b', 'c')
('c', 'c', 'a')
('c', 'c', 'b')
('c', 'c', 'c')
Answered By: I'mahdi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.