>>> list("Word to Split")
['W', 'o', 'r', 'd', ' ', 't', 'o', ' ', 'S', 'p', 'l', 'i', 't']
Answer from Greg Hewgill on Stack Overflow>>> 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]
How to split each string into words and collects all words in a new list.
How can I split a string when a character changes from uppercase to lowercase in Python?
How to add a new line after every nth word?
split() function splitting every character?
What Is an LTPO Display?
An LTPO (low-temperature polycrystalline oxide) display is a new display technology that enables devices like the OnePlus 9 and 9 Pro to vary the device's refresh rate to conserve battery life.
Do I Get a Charger in the Box With My OnePlus 9?
Yes, the OnePlus 9 series includes a fast 65W charger in the box for very fast wired charging. You can charge your device up to 50 percent in just 15 minutes or from empty to fully charged in about 30 minutes.
Does the OnePlus 9 Series Support Wireless Charging?
The OnePlus 9 series has varying degrees of wireless charging capabilities. The OnePlus 9 Pro supports 50W wireless charging, which requires a proprietary OnePlus wireless charger. With any third-party chargers, the device charges at a maximum of 15W.
The regular OnePlus 9 only supports 15W wireless charging, and the lowest end OnePlus 9R doesn't support wireless charging at all. It's important to note that specific markets, like India, do not carry the wireless charging-capable OnePlus 9 variant.
I'm working on a book I've downloaded from project Guttenberg and after doing some data cleaning I have ended up with a list of strings. Next I should split each string into words and collect all the words in a new list.
I have tried to use the split commando but somehow I end up with transforming each string into a list instead of a a string.
I'm sorry if I'm being vague. Please let me know if I should provide more infromation...
I have created the following function.
def splitter():
wordList = [s.split(" ") for s in book]
return wordList
book = splitter()
Somehow I end up with the following output when I type book[0] into the console:
['the',
'history',
'of',
'australian',
'exploration',
'from',
'1788',
'to',
'1888']
Instead of splitting the string and ending up with a list of strings I have created a list of lists.
What I want to end up with is:
In[] book[0]
Out[] the
and
in[] book[:9]
['the',
'history',
'of',
'australian',
'exploration',
'from',
'1788',
'to',
'1888']