>>> 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]
Is there a code line that can do that? Print sunny as 's','u','n','n','y'?
How to split a word into letters in Python - Stack Overflow
How to split each string into words and collects all words in a new list.
There is any way I can split a word into letters and put every word in a separate value of an array in Python? - Stack Overflow
python - How to split a string into letters - Stack Overflow
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']
in python, words are already lists, this means, they already have positions asigned, try: print input[0] and see if you want to assign a variable the value of any position in your string, just select the position as if it was a list: foo = input[#]
Try this:
def string_spliter(s):
result = []
for element in s:
result.append(element)
return result
print(string_spliter(string))
>>> 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']