Basically, I have this:
ls = [
'###',
'# a',
'#b#'
]
and want it to be like:
ls = [
['#', '#', '#'],
['#', ' ', 'a'],
['#', 'b', '#']
]
Anyone could help me out?
Basically, I have this:
ls = [
'###',
'# a',
'#b#'
]
and want it to be like:
ls = [
['#', '#', '#'],
['#', ' ', 'a'],
['#', 'b', '#']
]
Anyone could help me out?
Just convert each string to a list in a list comprehension
>>> [list(s) for s in ls]
[['#', '#', '#'], ['#', ' ', 'a'], ['#', 'b', '#']]
By using a list comprehension you can loop through your list and apply some operation on your list items. By putting these items into the list
operator you turn an iterable into a list. Because a string is basically a iterable of characters you can convert it into a list of characters with list
.
ls = [
'###',
'# a',
'#b#'
]
[list(x) for x in ls]
>> [['#', '#', '#'], ['#', ' ', 'a'], ['#', 'b', '#']]
print(list(map(list, ls))) # [['#', '#', '#'], ['#', ' ', 'a'], ['#', 'b', '#']]