def generate(chars, length, prefix=None):
    if length < 1:
        return
if not prefix:
    prefix = ''
for char in chars:
    permutation = prefix + char
if length == 1:
    yield permutation
else:
    for sub_permutation in generate(chars, length - 1, prefix=permutation):
        yield sub_permutation
Generate alphanumeric strings sequentially