An easy way to separate letters in a string and allocate them in a list is by using the function list()
python. Thus:
palavras = ['teste','teste','teste','teste','teste']
matriz = []
for palavra in palavras:
matriz.append(list(palavra))
With this, if you have display the contents of the list matriz
, the result will be exactly what you expect:
print(matriz)
[['t', 'e', 's', 't', 'e'],
['t', 'e', 's', 't', 'e'],
['t', 'e', 's', 't', 'e'],
['t', 'e', 's', 't', 'e'],
['t', 'e', 's', 't', 'e']]
The function list()
traverses each position of the object passed as parameter, transforms each of these positions into a new object and allocates within a list. The method .append()
adds the given parameter inside the list.
In that case, we turn every word 'teste'
on a list ['t','e','s','t','e']
and allocate within the list matriz
, which had previously been created.
Very cool this answer.
– user89389
thanks a lot! I spent a lot of time cracking my head with this list function.
– gui_lls