python list group by first character
Question: 
    list1=['hello','hope','hate','hack','bit','basket','code','come','chess']
    

What I need is:

    
    
    list2=[['hello','hope','hate','hack'],['bit','basket'],['code','come','chess']]
    

If the first character is the same and is the same group, then sublist it.

How can I solve this?


Answer: You can use
[`itertools.groupby`](http://docs.python.org/2/library/itertools.html#itertools.groupby):

    
    
    >>> from itertools import groupby
    >>> list1 = ['hello','hope','hate','hack','bit','basket','code','come','chess']
    >>> [list(g) for k, g in groupby(list1, key=lambda x: x[0])]
    [['hello', 'hope', 'hate', 'hack'], ['bit', 'basket'], ['code', 'come', 'chess']]
    

