pythonlistsortingsplitalphabetical

Python: Split list based on first character of word


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?


Solution

  • 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