118

Is there a function in python to split a word into a list of single letters? e.g:

s = "Word to Split"

to get

wordlist = ['W', 'o', 'r', 'd', ' ', 't', 'o', ' ', 'S', 'p', 'l', 'i', 't']
blackraven
  • 5,284
  • 7
  • 19
  • 45
gath
  • 2,286
  • 5
  • 19
  • 11
  • just check out this documentation: http://docs.python.org/library/stdtypes.html –  Nov 01 '11 at 18:09
  • 8
    Old thread, but it's worth mentioning: Most of the time you don't need to do this at all. The characters of a python string can be accessed as a list directly ie. `s[2]` is 'r', and `s[:4]` is 'Word' and `len(s)` is 13. You can iterate over them as well: `for char in s: print char` – domoarigato Nov 17 '14 at 14:19
  • @domoarrigato but due to different behavior of srting and list for mutability can be reason to do this. – brainLoop Apr 15 '18 at 07:38
  • def show_letters(word): for ch in word: print(ch) show_letters("Hello") – biddut Sep 02 '20 at 23:43

7 Answers7

260
>>> list("Word to Split")
['W', 'o', 'r', 'd', ' ', 't', 'o', ' ', 'S', 'p', 'l', 'i', 't']
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 10
    Any reason you know of why "Word to Split".split('') doesn't do the same thing. It doesn't, but really seems like it should. – Walter Nissen Aug 17 '10 at 04:59
  • 2
    @Walter Nissen: I get "ValueError: empty separator" when trying that. The empty regex is not terribly well defined. – Greg Hewgill Aug 17 '10 at 05:23
  • Is there no delimiter for using `split()` to get the characters? `split()` takes a second argument *maxsplits*, but there is no equivalent with `list()`. Of course there are work arounds... – Chris_Rands Sep 15 '16 at 09:29
28

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]
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
11

The list function will do this

>>> list('foo')
['f', 'o', 'o']
Mez
  • 24,430
  • 14
  • 71
  • 93
4

Abuse of the rules, same result: (x for x in 'Word to split')

Actually an iterator, not a list. But it's likely you won't really care.

Tim Ottinger
  • 1,442
  • 9
  • 5
  • Of course, `'Word to split'` used directly is also an iterable of its own characters, so the generator expression is just pointless wrapping. – ShadowRanger Jun 21 '16 at 23:30
2
text = "just trying out"

word_list = []

for i in range(len(text)):
    word_list.append(text[i])

print(word_list)

Output:

['j', 'u', 's', 't', ' ', 't', 'r', 'y', 'i', 'n', 'g', ' ', 'o', 'u', 't']
blackraven
  • 5,284
  • 7
  • 19
  • 45
Iris Chen
  • 31
  • 1
1

The easiest option is to just use the list() command. However, if you don't want to use it or it dose not work for some bazaar reason, you can always use this method.

word = 'foo'
splitWord = []

for letter in word:
    splitWord.append(letter)

print(splitWord) #prints ['f', 'o', 'o']
Random 375
  • 13
  • 5
0

def count(): list = 'oixfjhibokxnjfklmhjpxesriktglanwekgfvnk'

word_list = []
# dict = {}
for i in range(len(list)):
    word_list.append(list[i])
# word_list1 = sorted(word_list)
for i in range(len(word_list) - 1, 0, -1):
    for j in range(i):
        if word_list[j] > word_list[j + 1]:
            temp = word_list[j]
            word_list[j] = word_list[j + 1]
            word_list[j + 1] = temp
print("final count of arrival of each letter is : \n", dict(map(lambda x: (x, word_list.count(x)), word_list)))
pratiksha
  • 11
  • 2