improve this very-simple dictionary generator in python
Question: I'm trying to make a simple dict generator. It works but it isn't very
functional yet.

I'd like to improve it by being able to change the max size of the output
without touching the code.

    
    
    letr='abcdefghijklmnopqrstuvwxyz'
    for i in range(len(letr)):
        t=letr[i]
        print t
        for t2 in letr:
            print t+t2
            for t3 in letr:
                print t+t2+t3
                for t4 in letr:
                    print t+t2+t3+t4
                    for t5 in letr:
                        print t+t2+t3+t4+t5
    


Answer: 
    import itertools
    
    def dict_gen(n):
        letr = 'abcdefghijklmnopqrstuvwxyz'
        return itertools.chain(''.join(j) for i in range(n) 
                               for j in itertools.product(letr, repeat=i+1))
    

Usage:

    
    
    for word in dict_gen(n):  # replace n with the max word length you want
        print word
    

Unlike some of the other answers this will include duplicates like your
example ('aa', 'bb', etc).

`dict_gen()` will return a generator, but you can always just pass it into
`list()` if you need to access elements by index:

    
    
    >>> words = list(dict_gen(5))
    >>> len(words) == 26 + 26**2 + 26**3 + 26**4 + 26**5  # verify correct length
    True
    >>> words[20:30]  # transition from one letter to two letters
    ['u', 'v', 'w', 'x', 'y', 'z', 'aa', 'ab', 'ac', 'ad']
    >>> words[-10:]   # last 10 elements
    ['zzzzq', 'zzzzr', 'zzzzs', 'zzzzt', 'zzzzu', 'zzzzv', 'zzzzw', 'zzzzx', 'zzzzy', 'zzzzz']
    

