generating a liste of characters

Mark Tolonen metolone+gmane at gmail.com
Fri Dec 5 13:39:26 CET 2008


"Yves Dorfsman" <yves at zioup.com> wrote in message 
news:YywZk.17248$st1.2898 at newsfe10.iad...
> Is there any built in way to generate a list of characters, something
> along the line of range('a'-'z') ?
>
> Right now I am using:
>
>  chars  = [ chr(l)  for l in range(0x30, 0x3a) ] # 0 - 9
>  chars += [ chr(l)  for l in range(0x41, 0x5b) ] # A - Z
>  chars += [ chr(l)  for l in range(0x61, 0x7b) ] # a - z
>
> Is there a better, more straight forward way of doing that ?

Writing a helper function reduces code repetition and improves readability:

    def crange(startch,endch):
        '''Return a list of characters from startch to endch, inclusive.'''
        return [chr(c) for c in xrange(ord(startch),ord(endch)+1)]

    chars = crange('0','9') + crange('A','Z') + crange('a','z')

-Mark





More information about the Python-list mailing list