Python: Split list based on first character of word

Question:

Im kind of stuck on an issue and Ive gone round and round with it until ive confused myself.

What I am trying to do is take a list of words:

['About', 'Absolutely', 'After', 'Aint', 'Alabama', 'AlabamaBill', 'All', 'Also', 'Amos', 'And', 'Anyhow', 'Are', 'As', 'At', 'Aunt', 'Aw', 'Bedlam', 'Behind', 'Besides', 'Biblical', 'Bill', 'Billgone']

Then sort them under and alphabetical order:

A
About
Absolutely
After

B
Bedlam
Behind

etc…

Is there and easy way to do this?

Asked By: JasonOrtiz

||

Answers:

Use itertools.groupby() to group your input by a specific key, such as the first letter:

from itertools import groupby
from operator import itemgetter

for letter, words in groupby(sorted(somelist), key=itemgetter(0)):
    print letter
    for word in words:
        print word
    print

If your list is already sorted, you can omit the sorted() call. The itemgetter(0) callable will return the first letter of each word (the character at index 0), and groupby() will then yield that key plus an iterable that consists only of those items for which the key remains the same. In this case that means looping over words gives you all items that start with the same character.

Demo:

>>> somelist = ['About', 'Absolutely', 'After', 'Aint', 'Alabama', 'AlabamaBill', 'All', 'Also', 'Amos', 'And', 'Anyhow', 'Are', 'As', 'At', 'Aunt', 'Aw', 'Bedlam', 'Behind', 'Besides', 'Biblical', 'Bill', 'Billgone']
>>> from itertools import groupby
>>> from operator import itemgetter
>>> 
>>> for letter, words in groupby(sorted(somelist), key=itemgetter(0)):
...     print letter
...     for word in words:
...         print word
...     print
... 
A
About
Absolutely
After
Aint
Alabama
AlabamaBill
All
Also
Amos
And
Anyhow
Are
As
At
Aunt
Aw

B
Bedlam
Behind
Besides
Biblical
Bill
Billgone
Answered By: Martijn Pieters

Instead of using any library imports, or anything fancy.
Here is the logic:

def splitLst(x):
    dictionary = dict()
    for word in x:
       f = word[0]
       if f in dictionary.keys():
            dictionary[f].append(word)
       else:
            dictionary[f] = [word]
     return dictionary

splitLst(['About', 'Absolutely', 'After', 'Aint', 'Alabama', 'AlabamaBill', 'All', 'Also', 'Amos', 'And', 'Anyhow', 'Are', 'As', 'At', 'Aunt', 'Aw', 'Bedlam', 'Behind', 'Besides', 'Biblical', 'Bill', 'Billgone'])
Answered By: Mike

def split(n):
n2 = []
for i in n:
if i[0] not in n2:
n2.append(i[0])
n2.sort()
for j in n:
z = j[0]
z1 = n2.index(z)
n2.insert(z1+1, j)
return n2

word_list = [‘be’,’have’,’do’,’say’,’get’,’make’,’go’,’know’,’take’,’see’,’come’,’think’,
‘look’,’want’,’give’,’use’,’find’,’tell’,’ask’,’work’,’seem’,’feel’,’leave’,’call’]
print(split(word_list))