How to create a letter progression for a variable in a loop in Python?
<p>I am trying to change a variable (lett) to the next letter in the alphabet on each iteration of a loop. I have to also be able to set this variable at the beginning of the script to a certain letter (and the initial letter will vary depending on the use of the script). I started with creating the following bit of code:</p>

<p>Initial script (when I was still learning):</p>

<pre><code>while lett_trig == 2:
    if set_lett == 2:
        lett = "a"
    if set_lett == 3:
        lett = "b"
    if set_lett == 4:
        lett = "c"
    if set_lett == 5:
        lett = "d"
    if set_lett == 6:
        lett = "e"
    if set_lett == 7:
        lett = "f"
    if set_lett == 8:
        lett = "g"
    if set_lett == 9:
        lett = "h"
#... and this goes on till it reaches if set_let == 27: lett = "z"

    set_lett += 1
    if set_lett == 28:
        set_lett = 2
    print lett

# set_lett starts at two because I left set_lett == 1 to create a space (" ")
</code></pre>

<p>This is of course a simplification of a larger script.
<strong>This is the only simplification I could come up with:</strong> </p>

<pre><code>lett_trig = 2
x = 0

a = ["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"]

while lett_trig == 2:
    lett = a[x]
    x += 1
    if x == 26:
        x = 0
</code></pre>

<p>Is there any other way to mathematically change from one letter to another? Through some binary conversion operation? or is the list-way the most efficient?</p>

<hr>

<p>Answer: after going through all the answers and testing them for efficiency, I found the dict function to be the fastest (and cleanest). An example of the code:</p>

<pre><code>import string

letter_map = dict(zip(string.ascii_lowercase, string.ascii_lowercase[1:] + string.ascii_lowercase[0]))
lett1 = "d"

while ord(lett2) &lt; 122:
    print lett1
    lett1 = letter_map[lett1]
</code></pre>
