All possible combinations of dictionary values given input string. Python
Question: I'm trying to get all possible strings from the values in a dictionary given a
particular key. For example, 'A' could mean either 'aaa','aba', or 'aac' and
'B' could mean either 'bbb','bab', or 'bbc', etc.

I've given an example of the output where it shows most combinations of the
possible strings

    
    
    import itertools
    in_ = 'ABC'
    D = {'A':['aaa','aba','aac'],'B':['bbb','bab','bbc'],'C':['ccc','cac','ccb']}
    
    #out_ = ['aaabbbccc','ababbbccc','aacbbbccc','aaababccc','aaabbcccc','aaabbbcac','aaabbbccb'...]
    

I started writing the code but it started to get REALLY messy and I feel that
there is a way to use itertools or something more pythonic to achieve this
goal

    
    
    output = []
    for char in in_:
        out_string = ''
        while char:
            for v in D[char]:
                while v:
                    for char2 in in_:
                        out_string
    #not pythonic . . . 
    


Answer: Well, you've got `itertools` imported there. Let's use it! We want to take the
Cartesian product `D['A'] × D['B'] × D['C']`, so we'll do precisely that using
`itertools.product`.

    
    
    import itertools
    
    in_ = 'ABC'
    D = {'A':['aaa','aba','aac'],'B':['bbb','bab','bbc'],'C':['ccc','cac','ccb']}
    
    iterables = [D[character] for character in in_]
    out_ = [''.join(tup) for tup in itertools.product(*iterables)]
    

Now, `out_` is:

    
    
    ['aaabbbccc', 'aaabbbcac', 'aaabbbccb', 'aaababccc', 'aaababcac', 'aaababccb', 
     'aaabbcccc', 'aaabbccac', 'aaabbcccb', 'ababbbccc', 'ababbbcac', 'ababbbccb', 
     'abababccc', 'abababcac', 'abababccb', 'ababbcccc', 'ababbccac', 'ababbcccb', 
     'aacbbbccc', 'aacbbbcac', 'aacbbbccb', 'aacbabccc', 'aacbabcac', 'aacbabccb', 
     'aacbbcccc', 'aacbbccac', 'aacbbcccb']
    

Is that the result you were going for?

