Pythonic way to create list of address letter/numbers from an input address range like 1-12A
<p>Simple case: For a given string input like '1-12A', I'd like to output a list like</p>

<pre><code>['1A', '2A', '3A', ... , '12A']
</code></pre>

<p>That's easy enough, I could use something like the following code:</p>

<pre><code>import re

input = '1-12A'

begin = input.split('-')[0]                   #the first number
end = input.split('-')[-1]                    #the last number
letter = re.findall(r"([A-Z])", input)[0]     #the letter

[str(x)+letter for x in range(begin, end+1)]  #works only if letter is behind number
</code></pre>

<p>But sometimes I'll have cases where the input is like 'B01-B12', and I'd like the output to be like this:</p>

<pre><code>['B01', 'B02', 'B03', ... , 'B12']
</code></pre>

<p>Now the challenge is, what's the most pythonic way to create a function to can build up such lists from either of the above two inputs? It might be a function that accepts the begin, end and letter inputs, but it has to account for <a href="http://stackoverflow.com/questions/134934/display-number-with-leading-zeros">leading zeros</a>, and the fact that the letter could be in front or behind the number.</p>

<p>I'm not sure if there's a more <em>pythonic</em> way of doing it, but using some regexes and python's <a href="https://docs.python.org/2/library/stdtypes.html#str.format" rel="nofollow"><code>format</code></a> syntax, we can fairly easily deal with your inputs. Here is a solution :</p>

<pre><code>import re

def address_list(address_range):
    begin,end = address_range.split('-')     
    Nb,Ne=re.findall(r"\d+", address_range)

    #we deduce the paading from the digits of begin
    padding=len(re.findall(r"\d+", begin)[0]) 

    #first we decide whether we should use begin or end as a template for the ouput
    #here we keep the first that is matching something like ab01 or 01ab
    template_base = re.findall(r"[a-zA-Z]+\d+|\d+[a-zA-Z]+", address_range)[0]

    #we make a template by replacing the digits of end by some format syntax
    template=template_base.replace(re.findall(r"\d+", template_base)[0],"{{:0{:}}}".format(padding))

    #print("template : {} , example : {}".format(template,template.format(1)))

    return [template.format(x) for x in range(int(Nb), int(Ne)+1)]  

print(address_list('1-12A'))
print(address_list('B01-B12'))
print(address_list('C01-9'))
</code></pre>

<p><strong>Output:</strong></p>

<pre><code>['1A', '2A', '3A', '4A', '5A', '6A', '7A', '8A', '9A', '10A', '11A', '12A']
['B01', 'B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B09', 'B10', 'B11', 'B12']
['C01', 'C02', 'C03', 'C04', 'C05', 'C06', 'C07', 'C08', 'C09']
</code></pre>

python list functional-programming list-comprehension leading-zero