Search code examples
pythonlistprintingalphabeticalenumerate

Python start line with a letter when printing a list?


I want my code to print as

a. [value1]
b. [value2]
c. [value3]
d. [value4]
e. [value5]
f. [value6]

I currently have a simple counter set up in the form of

counter = 0
for key in sorted(word_counter):
    counter+=1
    print(counter, key, word_counter[key])

Word_counter is for another function I've already written that's finished I just want to fix how it prints. It's currently using numbers at the start but I want it to use letters instead like first example.

Forgot to add, once it hits z I want the next letters to be aa. bb. etc like so:

x. [value24]
y. [value25]
z. [value26]
aa. [value27]
bb. [value28]
cc. [value29]

each iteration through the alphabet adds a letter to the end.


Solution

  • This is pretty straightforward:

    >>> import string
    >>> from itertools import cycle
    >>> x = range(100)
    >>> letters = string.ascii_lowercase
    >>> m = len(letters)
    >>> for i, (let, e) in enumerate(zip(cycle(letters), x)):
    ...     print("{}. [{}]".format(let*(i//m+1), e))
    ...
    a. [0]
    b. [1]
    c. [2]
    d. [3]
    e. [4]
    f. [5]
    g. [6]
    h. [7]
    i. [8]
    j. [9]
    k. [10]
    l. [11]
    m. [12]
    n. [13]
    o. [14]
    p. [15]
    q. [16]
    r. [17]
    s. [18]
    t. [19]
    u. [20]
    v. [21]
    w. [22]
    x. [23]
    y. [24]
    z. [25]
    aa. [26]
    bb. [27]
    cc. [28]
    dd. [29]
    ee. [30]
    ff. [31]
    gg. [32]
    hh. [33]
    ii. [34]
    jj. [35]
    kk. [36]
    ll. [37]
    mm. [38]
    nn. [39]
    oo. [40]
    pp. [41]
    qq. [42]
    rr. [43]
    ss. [44]
    tt. [45]
    uu. [46]
    vv. [47]
    ww. [48]
    xx. [49]
    yy. [50]
    zz. [51]
    aaa. [52]
    bbb. [53]
    ccc. [54]
    ddd. [55]
    eee. [56]
    fff. [57]
    ggg. [58]
    hhh. [59]
    iii. [60]
    jjj. [61]
    kkk. [62]
    lll. [63]
    mmm. [64]
    nnn. [65]
    ooo. [66]
    ppp. [67]
    qqq. [68]
    rrr. [69]
    sss. [70]
    ttt. [71]
    uuu. [72]
    vvv. [73]
    www. [74]
    xxx. [75]
    yyy. [76]
    zzz. [77]
    aaaa. [78]
    bbbb. [79]
    cccc. [80]
    dddd. [81]
    eeee. [82]
    ffff. [83]
    gggg. [84]
    hhhh. [85]
    iiii. [86]
    jjjj. [87]
    kkkk. [88]
    llll. [89]
    mmmm. [90]
    nnnn. [91]
    oooo. [92]
    pppp. [93]
    qqqq. [94]
    rrrr. [95]
    ssss. [96]
    tttt. [97]
    uuuu. [98]
    vvvv. [99]