Question: I'm still learning to code in Python

I want to generate a string based on pattern, the only way I know is by using for loop.

In example code below, I create a loop for "vcvcv" pattern. c=consonant, v=vowel

How to create a dynamic loop, based on pattern that I provide to the script?

eg. if pattern is "cvcvc" the loop should be build to produce the string

Help appeciated.

Thanks.

```
#!/bin/env python

vowel="aeiou"
consonant="bcdfghjklmnpqrstvwxyz"

lvowel=list(vowel)
lconsonant=list(consonant)

# pattern for "vcvcv" = ababa
for a in lvowel:
  for b in lconsonant:
    for c in lvowel:
      for d in lconsonant:
            for e in lvowel:
                  myname=a+b+c+d+e
                  print myname

# pattern for "cvcvc" = babab
# how to make the loop dynamic based on pattern ?

```

Answer: 
Something like this should work:

```
import itertools

mapping = {
    'v': 'aeiou',
    'c': 'bcdfghjklmnpqrstvwxyz'
}

pattern = 'vcvcv'

for thing in itertools.product(*map(mapping.get, pattern)):
    print ''.join(thing)

```

Here's roughly how it works:

* `map(mapping.get, pattern)` just converts `'vcv'` to `['aeiou', 'bcdfghjklmnpqrstvwxyz', 'aeiou']`. It replaces each letter with the corresponding list of characters.
* `*map(...)` unpacks the argument list.
* `itertools.product()` is like a bunch of nested for loops.
* `''.join(thing)` joins the list of characters into a single string.

If you want to do this without `itertools`, you'll have to make a recursive function.
A somewhat more elaborate but easily customizable version for the first n permutations is given below,

```
def gen_pattern( seq, op = "" ):
      vowel="aeiou"
      consonant="bcdfghjklmnpqrstvwxyz"

      lvowel=list(vowel)
      lconsonant=list(consonant)
      if ( not seq ):
            print op
            return

      if ( seq[0] == 'v' ):
            for v in lvowel:
                  gen_pattern( seq[1:], op+v )
      elif ( seq[0] == 'c' ):
            for c in lconsonant:
                  gen_pattern( seq[1:],op+c )

if __name__ == "__main__":
      gen_pattern("vcvcv")

```

I agree it is more work though!