Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List comprehension for first 10 alphabets [duplicate]

Tags:

python

list

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?

like image 329
liv2hak Avatar asked Sep 03 '25 02:09

liv2hak


1 Answers

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']
like image 68
Avinash Raj Avatar answered Sep 05 '25 08:09

Avinash Raj