Search code examples
pythonpython-3.xpython-itertools

Working with itertools.product and lists in python 3


I'm trying to create a possible list of codons given a protein sequence.

Basically, the script i'm trying to create will process a given string input and outputs a possible combinations of another set of strings the input represents.

For example, the character 'F' represents either 'UUU' or 'UUC'; the character 'I' represents either 'AUU', 'AUC', or 'AUA'.

Given the input 'FI', the script I'm trying to create should output: 'UUUAUU', 'UUUAUC', 'UUUAUA', 'UUCAUU', 'UUCAUC', and 'UUCAUA'.

I'm currently stuck with this code:

import itertools

F = ['UUU', 'UUC']
I = ['AUU', 'AUC', 'AUA']

seq, pool = 'FI', []

for i in seq:
   pool.append(eval(i))

for n in itertools.product(pool):
   print(n)

It works when I replace pool in itertools.product with pool[0], pool[1]. But I can't figure out how to make it work so that the user can input a string with more than 2 character (i.e. not to make it hard-coded).

Thanks in advance for the help!


Solution

  • You can use *pool to "unpack" the list when calling product():

    for n in itertools.product(*pool):
       print(n)
    

    This syntax expands the list pool into separate positional parameters.