Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a list of strings into their individual characters Python

Tags:

python

split

Let's say I have a list:

list = ['foo', 'bar', 'bak']

I want to split these strings into their characters so I can assign values to each character (I.E. f = 5, o = 15, and so on).

How might I decompose these lists? I was thinking of turning each element into its own list, and referring to each element as an item of that list, but I am not sure how to go about doing that.

like image 737
Derp Avatar asked Aug 07 '26 08:08

Derp


1 Answers

Strings are iterable in Python, so you can just loop through them like this:

list = ['foo', 'bar', 'bak']

for item in list:
    for character in item:
        print(character)
like image 107
Acontz Avatar answered Aug 09 '26 00:08

Acontz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!