small_angel
small_angel

Reputation: 81

convert a list of characters to a list of ascii value

Is there a build in method to transform a list of characters to a list of ascii values of those characters?

For example, The method should transform ['a','b','c'] to [ord('a'),ord('b'),ord('c')].

Upvotes: 0

Views: 303

Answers (3)

Alexandr Shurigin
Alexandr Shurigin

Reputation: 3981

Don't think that Python has a build in method, but that's pretty easy to write a "one-liner".

source = ['a', 'b', 'c']

print([ord(c) for c in source])

outputs

[97, 98, 99]

Upvotes: 4

ExpDev
ExpDev

Reputation: 65

You can use map to call the ord function on all of the entries.

result = list(map(ord, ['a','b','c']))
print(results)

Upvotes: 0

oppressionslayer
oppressionslayer

Reputation: 7204

If you have python 3 you can do this with the bytes function:

list(bytes(''.join(['a','b','c']).encode()))

# [97, 98, 99]

Upvotes: 0

Related Questions