I am trying to split strings within nested lists, but for whatever reason, the strings do not split as I expect them to. For example, here is a nested list where I have strings that I want to split.
myList=[['letters', ['a b c d e', 'f g h i j']], ['digits', ['0 1 2 3 4', '5 6 7 8 9']]]
I have tried using for
loops to split each string, but it doesn't seem to work. I tried using the following code:
for i in myList:
for j in i[1]:
j=j.split()
However, it seems to do nothing to the list. I expect that after running the code, myList
should contain
[['letters', [['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h', 'i', 'j']]], ['digits', [['0', '1', '2', '3', '4'], ['5', '6', '7', '8', '9']]]]
If I look at each item using print()
and repr()
, I see the strings that I expect.
for i in myList2:
for j in i[1]:
print(repr(j))
prints out
'a b c d e'
'f g h i j'
'0 1 2 3 4'
'5 6 7 8 9'
as I expect, so I am unsure why the split values cannot be written.