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: 
If you're just getting into programming and want to see a more general solution than the itertools one listed above, then recursion is your best bet, allowing you to arbitrarily nest loops.

There is a slight complication here, which you could use [Python generators](http://wiki.python.org/moin/Generators) for, or else use simpler (but messier) constructs. An example of the latter is shown below.

Something like

```
def continuePattern(pat, strSoFar):
  if pat == '':
    print strSoFar
  elif pat[0] == 'v':
    for c in lvowel:
       continuePattern(pat[1:], strSoFar + c)
  elif pat[0] == 'c':
    for c in lconsonant:
       continuePattern(pat[1:], strSoFar + c)

```

This is one of several possible implementations, and one of the two most naive ones I can imagine.
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!