I’m parsing through a list in Python 3.7.9 that contains integers and strings. And I want to replace each string with the ASCII values of its chars.
As an example: operand = [200, 42, 0, 'A', 'BC']
The result I would like to have: operand = [200, 42, 0, 65, 66, 67]
The best I’ve come so far is this, where I redefine the list with nested comprehension:
operand = [[ord(char) for char in data] if type(data) is str else data for data in operand]
The problem is that it generates a mixed list: integers and nested lists of integers.
I would want to avoid flattening the list after.
How can I generate the correct list with nested comprehension?
1