Expand alphabetical range to list of characters in Python

alphabet, alphabetical, character, python, string

Solution

In [19]: s = 'B-F'

In [20]: list(map(chr, range(ord(s[0]), ord(s[-1]) + 1)))
Out[20]: ['B', 'C', 'D', 'E', 'F']

The trick is to convert both characters to their ASCII codes, and then use `range()`.

P.S. Since you require a list, the `list(map(...))` construct can be replaced with a list comprehension.

Problem

I have strings describing a range of characters alphabetically, made up of two characters separated by a hyphen. I'd like to expand them out into a list of the individual characters like this: ``` 'a-d' -> ['a','b','c','d'] 'B-F' -> ['B','C','D','E','F'] ``` What would be the best way to do this in Python?

Original source