0

I am unsure how to return all possible sequences within a list of lists. It is important that the index in the main list corresponds to the position in the sequence.

Input:

list = [["Lys", "Arg"],['Gly'],["Phe", "Tyr"]]

Output:

["Lys", "Gly", "Phe"]
["Arg", "Gly", "Phe"]
["Lys", "Gly", "Tyr"]
["Arg", "Gly", "Tyr"]

Thanks in advance.

  • `list` is not a variable name. It is a type. Don't overwrite it. – Kraigolas Feb 26 '21 at 00:54
  • @Kraigolas *[shadow](https://en.wikipedia.org/wiki/Variable_shadowing), not overwrite. But yes, you're right. See [this question](https://stackoverflow.com/q/31087111/4518341) for an example of what can happen. – wjandrea Feb 26 '21 at 01:09

1 Answers1

0

Try this:

import itertools

x = []
a = [["Lys", "Arg"],['Gly'],["Phe", "Tyr"]]
for i in itertools.product(*a):
    x.append(i)
ll2 = [list(y) for y in x]
print (*ll2)
Jatin Morar
  • 164
  • 1
  • 7
  • I think I see what you're saying however your output provides a list of tuples. – Jatin Morar Feb 26 '21 at 01:28
  • That for-loop is unnecessary. Just use `x = list(itertools.product(*a))`. To convert the elements to list, you could do `x = list(map(list, itertools.product(*a)))` – wjandrea Feb 26 '21 at 01:28
  • sorry, i edited my comment but that was confusing so I deleted it and reposted – wjandrea Feb 26 '21 at 01:32