Find all permutations of a string that is variable only at specific positions in Python

Question:

I have a DNA sequence which is variable only at specific locations and need to find all possible scenarios:

DNA_seq='ANGK' #N can be T or C  and K can be A or G
N=['T','C']
K=['A','G']

Results:

['ATGA','ATGG','ACGA','ACGG']

Searching online, It seems permutations method from itertools can be used for this purpose but not sure how it can be implemented. I would appreciate any help.

Asked By: Masih

||

Answers:

Use itertools.product:

from itertools import product
result = [f'A{n}G{k}' for n, k in product(N, K)]

Result:

['ATGA', 'ATGG', 'ACGA', 'ACGG']
Answered By: Vladimir Fokow
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.