cemshid
cemshid

Reputation: 43

Iterate combinations of characters in multiple lists where the output string is in a specific order

I'm trying to create a Python script with itertools where:

I have 4 lists, each containing single characters:

li1 = ["a", "b", "c"]
li2 = ["d", "e", "f"]
li3 = ["q", "w", "e"]
li4 = ["t", "y"]

and I would like to output all possible permutations of the following, in this specific order:

li1 + li2 + li3 + li1 + li4 + li1

where li is a string/character in the list, and the sequence/order cannot be changed

Being a novice all I can think of is iterating through each list as a loop but I don't know how to do it 4 times concurrently

Any help would be much appreciated

Upvotes: 0

Views: 83

Answers (1)

NPE
NPE

Reputation: 500257

You could use itertools.product() for this:

import itertools

li1 = ["a","b","c"]
li2 = ["d","e","f"]
li3 = ["q","w","e"]
li4 = ["t", "y"]

for elem in itertools.product(li1, li2, li3, li1, li4, li1):
  print elem

(I've taken the liberty of changing your sets into lists. The code, however, will also work with sets, except that order of permutations will be different.)

Upvotes: 3

Related Questions