How to get all possible combinations, given the sequence of keys from a dictionary with list values

I have, for example, this dictionary

d={'M':['ATG'],'D':['GAC','GAT'],'E':['GAA','GAG']} 

What I would like to receive as the output given by the sequence of keys is a list with all possible sequences. (there may also be a line in which all possible sequences will be on separate lines "\ n")

 sequence = "MDE" 

So, the conclusion should be as follows:

 ['ATGGACGAA','ATGGACGAG','ATGGATGAA','ATGGATGAG'] 

What I have tried so far is the following, but of course this is not what I want:

 seq_trans = '' for aa in sequence: for k, v in d.iteritems(): if k == aa: for item in v: seq_trans= seq_trans + item print seq_trans 

And what I get for "MDE":

 'ATGGACGATGAAGAG' 
+4
source share
1 answer

Here you can use itertools.product , it returns the Cartesian product of the input iterations.

 In [78]: seq="MED" In [79]: ["".join(x) for x in product(*(d[y] for y in seq))] Out[79]: ['ATGGAAGAC', 'ATGGAAGAT', 'ATGGAGGAC', 'ATGGAGGAT'] 
+7
source

All Articles