Search code examples
pythonstringcombinationspython-itertools

Generating all possible combinations of characters in a string


Say I have a string list:

li = ['a', 'b', 'c']

I would like to construct a new list such that each entry of the new list is a concatenation of a selection of 3 entries in the original list. Note that each entry can be chosen repeatedly:

new_li=['abc', 'acb', 'bac', 'bca', 'cab', 'cba', 'aab', 'aac',....'aaa', 'bbb', 'ccc']

The brutal force way is to construct a 3-fold nested for loop and insert each 3-combination into the new list. I was wondering if there is any Pythonic way to deal with that? Thanks.

Update: Later I will convert the new list into a set, so the order does not matter anyway.


Solution

  • This looks like a job for itertools.product.

    import itertools
    
    def foo(l):
         yield from itertools.product(*([l] * 3)) 
    
    for x in foo('abc'):
         print(''.join(x))
    
    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
    

    yield from is available to you from python3.3 and beyond. For older version, yield within a loop:

    def foo(l):
         for i in itertools.product(*([l] * 3)) :
             yield i