I have a list of lists of different lengths, containing some strings.
list_01 = ['ba', '4', 'cdf']
list_02 = ['a4a', '56', 'scdf']
...
list_N = ['baaa', 'dgg', '12f']
and I need to split the strings containing multiple characters in single ones, so that I get something like:
list_01 = ['b', 'a', '4', 'c', 'd', 'f']
list_02 = ['a', '4', 'a', '5' '6', 's', 'c','d', 'f']
...
list_N = ['b', 'a', 'a', 'a', 'd', 'g', 'g', '1', '2', 'f']
Now, I tried this piece of code:
for lst in [list_1, list_2, ... list_N]:
tmp = []
for x in lst:
if len(x)>1:
tmp.append([char for char in x])
else:
tmp.append(x)
lst = [item for sublist in tmp for item in sublist]
This works within the loop, i.e. each instance of lst is actually what I want, but the changes aren't transferred to list_01...list_N.
I assume there's some point in the code where lst stops being a reference to the current item in the for loop, but it becomes a new object altogether...probably here?
lst = [item for sublist in tmp for item in sublist]
I checked this discussion, and the ways to create a different copy of the list that are mentioned there don't seem to include anything I'm doing in my code. How can I fix this?