>>> s = "foobar"
>>> list(s)
['f', 'o', 'o', 'b', 'a', 'r']
Answer from iblazevic on Stack Overflow>>> s = "foobar"
>>> list(s)
['f', 'o', 'o', 'b', 'a', 'r']
In Python string is iterable. This means it supports special protocol.
>>> s = '123'
>>> i = iter(s)
>>> i
<iterator object at 0x00E82C50>
>>> i.next()
'1'
>>> i.next()
'2'
>>> i.next()
'3'
>>> i.next()
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
i.next()
StopIteration
list constructor may build list of any iterable. It relies on this special method next and gets letter by letter from string until it encounters StopIteration.
So, the easiest way to make a list of letters from string is to feed it to list constructor:
>>> list(s)
['1', '2', '3']
Is there a function in python to split a word into a list? - Stack Overflow
How to split one word into an array?
python - How to convert words inputs to a list - Stack Overflow
python - How can I convert single word string to a list? - Stack Overflow
I think this is the simplest way for anyone else stumbling on this post given the late response:
>>> string = 'This is a string, with words!'
>>> string.split()
['This', 'is', 'a', 'string,', 'with', 'words!']
Try this:
import re
mystr = 'This is a string, with words!'
wordList = re.sub("[^\w]", " ", mystr).split()
How it works:
From the docs :
re.sub(pattern, repl, string, count=0, flags=0)
Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. If the pattern isn’t found, string is returned unchanged. repl can be a string or a function.
so in our case :
pattern is any non-alphanumeric character.
[\w] means any alphanumeric character and is equal to the character set [a-zA-Z0-9_]
a to z, A to Z , 0 to 9 and underscore.
so we match any non-alphanumeric character and replace it with a space .
and then we split() it which splits string by space and converts it to a list
so 'hello-world'
becomes 'hello world'
with re.sub
and then ['hello' , 'world']
after split()
let me know if any doubts come up.
>>> list("Word to Split")
['W', 'o', 'r', 'd', ' ', 't', 'o', ' ', 'S', 'p', 'l', 'i', 't']
The easiest way is probably just to use list(), but there is at least one other option as well:
s = "Word to Split"
wordlist = list(s) # option 1,
wordlist = [ch for ch in s] # option 2, list comprehension.
They should both give you what you need:
['W','o','r','d',' ','t','o',' ','S','p','l','i','t']
As stated, the first is likely the most preferable for your example but there are use cases that may make the latter quite handy for more complex stuff, such as if you want to apply some arbitrary function to the items, such as with:
[doSomethingWith(ch) for ch in s]
I have a large data set, a list of 10,000 words that I need to turn into strings and then put them into a list.
The words have been copied and pasted into the IDE and are each on a new line eg:
words = '''a b c d '''
for now I'm using triple quotes to keep them together, but how can I turn this into a useable list?