In other languages, I would use a construct like this:
a..z
I couldn't come up with a better solution than this:
[chr(x) for x in range(ord("a"), ord("z") + 1)]
Is there a shorter, more readable way of building such a list ?
Not necessarily great if you want to do something other than a to z, but you can do this:
from string import ascii_lowercase
for c in ascii_lowercase:
print c
My way is similar to yours but you can use map
and create an arbitrary function like this
>>> def generate_list(char1,char2):
... myl = map(chr, range(ord(char1),ord(char2)+1))
... print myl
...
>>> generate_list("a","d")
['a', 'b', 'c', 'd']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With