9

Haskell can this:

['a'..'z']

In Python:

map(chr, range(97, 123))

I feel Python is something verbose. Have Python another easy way like Haskell?

thanksnote
  • 1,012
  • 11
  • 9

5 Answers5

13

Yes. from string import ascii_lowercase.

Julian
  • 3,375
  • 16
  • 27
6
from string import ascii_lowercase
L = list(ascii_lowercase)
Dair
  • 15,910
  • 9
  • 62
  • 107
  • It works both Python 2 and 3. – jfs Aug 20 '12 at 03:57
  • @J.F.Sebastian: Oh, woops, funny I was testing it on my computer doing some slightly different code and that's why it didn't work for me on Python 3 xD – Dair Aug 20 '12 at 03:59
6

A slightly more readable version without importing anything

alphabet = [chr(i) for i in range(ord('a'), ord('z') + 1)]
gsk
  • 558
  • 5
  • 12
1

There is no generic way, because syntax [a..b] in haskell uses special typeclass Enum, but there is no special magic methods or special syntax for enumeration in python.

Fedor Gogolev
  • 10,391
  • 4
  • 30
  • 36
  • Also, although I can think of *some* uses for this syntax, most potential use cases are completely covered by `range(start, stop, step)` in python. – Joel Cornett Aug 20 '12 at 04:03
0

Since you want it as a list, then you can do the following:

from string import ascii_lowercase
l = [i for i in ascii_lowercase]
Makoto
  • 104,088
  • 27
  • 192
  • 230