Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python can make alphabet list like Haskell? [duplicate]

Tags:

python

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?

like image 685
thanksnote Avatar asked Aug 20 '12 03:08

thanksnote


People also ask

How to use the alphabet in Python?

1 General Approach for Python Alphabet. The ASCII value of A-Z lies in the range of 65-90, and the for a-z, the value is in the range 97 – 122. 2 Python Alphabet using list comprehension. ... 3 Python Alphabet using map function. ... 4 Importing the String Module. ...

How do I make a uppercase alphabet list in Python?

If you prefer the alphabet list to be in uppercase, then you should use string.ascii_uppercase and reuse the code above and will produce the same output, but in uppercase format. range () is a function that outputs a series of numbers. You can specify when the function starts and stops with the first and second arguments.

How to store 26 lowercase characters in a Python list?

In this tutorial, we want to store the English alphabet’s 26 lowercase characters in a Python list. The quickest way to solve this problem is by making use of the ASCII values of each character and using pre-existing functions in Python.

How do I create a list in Python?

To create a list in Python, we use square brackets ( [] ). Here's what a list looks like: ListName = [ListItem, ListItem1, ListItem2, ListItem3, ...] Note that lists can have/store different data types. You can either store a particular data type or mix them. In the next section, you'll see how to add items to a list.


2 Answers

A slightly more readable version without importing anything

alphabet = [chr(i) for i in range(ord('a'), ord('z') + 1)]
like image 78
gsk Avatar answered Oct 19 '22 22:10

gsk


Yes. from string import ascii_lowercase.

like image 40
Julian Avatar answered Oct 19 '22 22:10

Julian