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']
4
  • just check out this documentation: docs.python.org/library/stdtypes.html
    – user1024267
    Nov 1, 2011 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 Nov 17, 2014 at 14:19
  • @domoarrigato but due to different behavior of srting and list for mutability can be reason to do this.
    – brainLoop
    Apr 15, 2018 at 7:38
  • def show_letters(word): for ch in word: print(ch) show_letters("Hello")
    – biddut
    Sep 2, 2020 at 23:43

7 Answers 7

260
>>> list("Word to Split")
['W', 'o', 'r', 'd', ' ', 't', 'o', ' ', 'S', 'p', 'l', 'i', 't']
3
  • 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. Aug 17, 2010 at 4:59
  • 2
    @Walter Nissen: I get "ValueError: empty separator" when trying that. The empty regex is not terribly well defined. Aug 17, 2010 at 5: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... Sep 15, 2016 at 9: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]
11

The list function will do this

>>> list('foo')
['f', 'o', 'o']
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.

1
  • Of course, 'Word to split' used directly is also an iterable of its own characters, so the generator expression is just pointless wrapping. Jun 21, 2016 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']
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']
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)))

Not the answer you're looking for? Browse other questions tagged or ask your own question.