4

I am trying to generate a list comprehension for first 10 letters.

letters = [l for l in 'a-j']
print(letters)

Output:

['a', '-', 'h']

This doesn't produce the expected output.How do I generate a list of first 10 alphabets?

liv2hak
  • 14,472
  • 53
  • 157
  • 270

1 Answers1

8

Use string.ascii_lowercase which contains the lowercase alphabets stored by default.

>>> import string
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> list(string.ascii_lowercase[:10])
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • 2
    Because OP is trying to figure out how to get the chars between `a` and `j`, maybe `list(string.ascii_lowercase[string.ascii_lowercase.index('a'):string.ascii_l owercase.index('f')+1])` would be more clear? – Remi Guan Dec 05 '15 at 04:24