Is there a simple function to generate a list like ['a', 'b', 'c', ... 'z']?

G

Guest

Is there a simple function to generate a list like ['a', 'b', 'c', ...
'z']? The range() just can generate the numeric list.
 
M

Michael Bentley

Is there a simple function to generate a list like ['a', 'b', 'c', ...
'z']? The range() just can generate the numeric list.


import string
list(string.lowercase)
 
G

Guest

äººè¨€è½æ—¥æ˜¯å¤©æ¶¯ï¼Œæœ›æžå¤©æ¶¯ä¸è§å®¶ said:
Is there a simple function to generate a list like ['a', 'b', 'c', ...
'z']? The range() just can generate the numeric list.

There is:
[ chr(i) for i in range(97, 123) ]

Thomas
 
7

7stud

Is there a simple function to generate a list like ['a', 'b', 'c', ...
'z']?   The range() just can generate the numeric list.

Not very simple, but how about a list comprehension:

import string

lst = [char for char in string.letters[:26] ]
print lst
 
G

Guest

äººè¨€è½æ—¥æ˜¯å¤©æ¶¯ï¼Œæœ›æžå¤©æ¶¯ä¸è§å®¶ said:
Is there a simple function to generate a list like ['a', 'b', 'c', ...
'z']?   The range() just can generate the numeric list.

There is:
[ chr(i) for i in range(97, 123) ]

Thomas

Thanks you too! I'm a beginner of python.
 
D

Duncan Booth

Michael Bentley said:
Is there a simple function to generate a list like ['a', 'b', 'c', ...
'z']? The range() just can generate the numeric list.


import string
list(string.lowercase)
Be careful here. If you change locale that will return all lowercase
letters not just 'a' to 'z'. For example:
 
S

Steven D'Aprano

Is there a simple function to generate a list like ['a', 'b', 'c', ...
'z']?   The range() just can generate the numeric list.

Not very simple, but how about a list comprehension:

import string

lst = [char for char in string.letters[:26] ]
print lst

Anytime you write a list comp like [x for x in thing] that should be a
warning that you shouldn't be writing a list comp.

lst = list(string.letters[:26])
 
S

skip

Thomas> [ chr(i) for i in range(97, 123) ]

Or with fewer magic numbers:

[chr(i) for i in range(ord('a'), ord('z')+1)]

Skip
 
M

Michael Bentley

Michael Bentley said:
Is there a simple function to generate a list like ['a', 'b',
'c', ...
'z']? The range() just can generate the numeric list.


import string
list(string.lowercase)
Be careful here. If you change locale that will return all lowercase
letters not just 'a' to 'z'. For example:
abcdefghijklmnopqrstuvwxyzƒšœžªµºßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ

Thanks, Duncan -- that would have eventually bitten me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top