Python: Pair alphabets after loop is completed
Question: I tried to pair Alphabets by this

    
    
    import string
    a=string.uppercase
    for i in range(0,30):
        print a[i%26]*(i / 26+1)
    
    This will print A-Z and then after Z it will print AA BB like string
    

but i need to put this string like `AA AB AC AD AE` until the range is defined
after printing A-Z then the result will be like print A-Z then AA AB AC ....


Answer: You can take advantage of the `itertools` module and use a generator to handle
this pretty cleanly:

    
    
    from itertools import count, product, islice
    from string import ascii_uppercase
    
    def multiletters(seq):
        for n in count(1):
            for s in product(seq, repeat=n):
                yield ''.join(s)
    

gives

    
    
    >>> list(islice(multiletters('ABC'), 20))
    ['A', 'B', 'C', 'AA', 'AB', 'AC', 'BA', 'BB', 'BC', 'CA', 'CB', 'CC', 'AAA', 'AAB', 'AAC', 'ABA', 'ABB', 'ABC', 'ACA', 'ACB']
    >>> list(islice(multiletters(ascii_uppercase), 30))
    ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD']
    

and you can make an object and get them one by one, if you'd prefer:

    
    
    >>> m = multiletters(ascii_uppercase)
    >>> next(m)
    'A'
    >>> next(m)
    'B'
    >>> next(m)
    'C'
    

[Update: I should note though that I pass data between Python and Excel all
the time -- am about to do so, actually -- and never need this function. But
if you have a specific question about the best way to exchange data, it's
probably better to ask a separate question than to edit this one now that
there are several answers to the current question.]

