4

How can I iterate through the alphabet and have it continue past the letter Z? For example - a,b,c ... y,x,aa,ab,ac,ad

At the momenent this is my array

letters = [
           "a","b","c","d","e","f","g","h","i","k",
           "l","m","n","o","p","q","r","s","t","u",
           "v","w","x","y","z", "aa", "ab", "ac", "ad", "ae",
           "af", "ag", "ah", "ai", "aj", "ak", "al", "am", "an",
           "ao", "ap", "aq", "ar", "as", "at", "au", "av","aw", "ax", "ay",
           "az", "ba", "bb", "bc", "bd", "be", "bf", "bg", "bh", "bi",
           "bj", "bk", "bl", "bm", "bn", "bo", "bp", "bq", "br", "bs", "bt",
           "bu", "bv","bw", "bx", "by", "bz","ca","cb","cc","cd","ce",
           "cf","cg","ch","ci","cj","ck","cl","cm","cn","co","cp",
           "cq","cr","cs","ct","cu","cv","cw"]

but I want to make it created within a loop.

GalahadXVI
  • 759
  • 1
  • 8
  • 19
  • What have you tried already? – stelioslogothetis May 11 '17 at 14:34
  • This is the general algorithm, in C#: http://stackoverflow.com/a/182924/2436175. The key is dividing by 26 converting the remainder into a letter at every step, until the division return 0. Use `%` (modulo) to get the remainder, `//` (floor division) to update the value for the next step. – Antonio May 11 '17 at 14:35
  • @Antonio I think you'll agree that Uriel's second itertools-based solution is a lot cleaner than messing around with modular arithmetic. – PM 2Ring May 11 '17 at 14:51
  • Do you really want them in a list? I think you'll find it much more convenient to have a generator that produces these strings for you. – PM 2Ring May 11 '17 at 14:54
  • @PM2Ring I agree. Since in the meantime I had the solution ready, I posted it [here](http://stackoverflow.com/a/43918966/2436175). It shouldn't harm :) – Antonio May 11 '17 at 14:56

3 Answers3

7

You can try this:

import string

letters = list(string.ascii_lowercase)

letters.extend([i+b for i in letters for b in letters])

print letters

By using a double for loop, we iterate over the alphabet like we normally would, except we can put the iteration in list comprehension, to save space and be more pythonic.

Ajax1234
  • 69,937
  • 8
  • 61
  • 102
5

since string.ascii_lowercase holds all of the lower-case letters, you can iterate through them in one loop (for 1-letter-string) or two (for 2-letters-string) and just add them to a list, like this:

import string
list = []
for c in string.ascii_lowercase: 
    list.append(c)

for c1 in string.ascii_lowercase:
    for c2 in string.ascii_lowercase:
        list.append(c1+c2)

print(list)
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
  • 1
    Instead of just code dumping why not explain what your program does? Right now it's not really a complete answer. – MooingRawr May 11 '17 at 14:41
2

string module would do the trick.

import string
alphabets=list(string.ascii_lowercase)
output=[] # output list
for a in alphabets:
     output.append(a)
for a in alphabets:
     for b in alphabets:
         output.append('%s%s' % (a,b))
print output