+2 votes
in Programming Languages by (73.8k points)
I have the following list:

aa=['a','b','c','d','e']

I want to replace its elements with integer values (e.g. ASCII values). e.g. aa = [97, 98, 99, 100, 101]

I can use 'for' loop to convert each element one by one. But can it be done in a better pythonic way?

1 Answer

+1 vote
by (348k points)
selected by
 
Best answer

You can use map() and ord() functions to avoid 'for' loop. Here is an example:

>>> aa
['a', 'b', 'c', 'd', 'e']
>>> t=map(lambda x:ord(x),aa)
>>> t
[97, 98, 99, 100, 101]
 


...