Yuce's Blog

Cognitive dissonance is yoga for the brain.*

Generating All Strings of a Given Alphabet

2005-06-10

For a recent research project, I had to write a function that generates all the strings of a given alphabet. It was (as always) very easy to figure it out in Python, only 5 minutes; below is the code (with mod­i­fi­ca­tions, I've polished the idea a bit):

def allstrings(alphabet, length):
    """Find the list of all strings of 'alphabet' of length 'length'"""

    if length == 0: return []

    c = [[a] for a in alphabet[:]]
    if length == 1: return c

    c = [[x,y] for x in alphabet 
continue.

python, strings, combination