Split list of strings into constituent characters

All we need is an easy explanation of the problem, so here it is.

I have these lines of code to split labels into characters and put them into a list.

Labels=[226md, 22d5n, 2356g, 23mdg, 23n88, 243mm,...]

def split(string):
    for i in range(len(labels)):
        return [char for char in string[i]]
for i in range(len(labels)):
    x=split(labels)
    print(x)

However, my output comes out as

['2', '2', '6', 'm', 'd']
['2', '2', '6', 'm', 'd']
['2', '2', '6', 'm', 'd']
['2', '2', '6', 'm', 'd']
['2', '2', '6', 'm', 'd']
['2', '2', '6', 'm', 'd']
...

instead of

['2', '2', '6', 'm', 'd']
['2', '2', 'd', '5', 'n']
['2', '3', '5', '6', 'g']
['2', '3', 'm', 'd', 'g']
['2', '3', 'n', '8', '8']
['2', '4', '3', 'm', 'm']
...

Each string is consistently 5 characters long. How can I get the preferred output so I can split each label into its constituent characters?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

This should work:

Labels=['226md', '22d5n', '2356g', '23mdg', '23n88', '243mm']

x = [[j for j in i] for i in Labels]

Value of x:

[['2', '2', '6', 'm', 'd'],
 ['2', '2', 'd', '5', 'n'],
 ['2', '3', '5', '6', 'g'],
 ['2', '3', 'm', 'd', 'g'],
 ['2', '3', 'n', '8', '8'],
 ['2', '4', '3', 'm', 'm']]

Or if you just want to print the output:

Labels=['226md', '22d5n', '2356g', '23mdg', '23n88', '243mm']

for i in Labels:
    print([j for j in i])

Output:

['2', '2', '6', 'm', 'd']
['2', '2', 'd', '5', 'n']
['2', '3', '5', '6', 'g']
['2', '3', 'm', 'd', 'g']
['2', '3', 'n', '8', '8']
['2', '4', '3', 'm', 'm']

Also, it is worth noting that you can swap [j for j in i] for just list(i). The reason your code does not work is because your return statement halts the execution of the code. Your split function (its name should be changed as split is a built in python function) could just be changed to this:

def split(string):
    return [list(i) for i in string]

And then change your for loop to just split(Labels).

Method 2

This happens because in the body of the method split you are always returning the characters of the first label. In fact the return statement halts the execution of the code and returns immidiately.

To achive the result you are describing you should save the rows into a cumulative variable and then return it when the loop has finished:

Labels=["226md", "22d5n", "2356g", "23mdg", "23n88", "243mm",...]

def split(labels):
    result = []
    for i in range(len(labels)):
        result.append([char for char in string[i]])
    return result

split(Labels)

Plus, this way you don’t need the last loop outside the function body because you are already iterating through all labels into the split function.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply