iterating through nested lists in python
Question: I'm trying to iterate through a list and depending on several conditions to
rearrange the items in the list in sublists, all inside the original list that
is. so with the code below in Python, while the list1 prints correctly by
grouping 0s, 1s and 2s :

    
    
    new list 1 = [['A0', 'B0', 'C0'], ['A1', 'B1', 'C1'], ['A2', 'B2', 'C2']]
    

everything gets mixed up on the 2nd list trying to re-group in further
sublists As Bs Cs by getting :

    
    
    new list 2 = [[['A0', 'A1', 'A2'], ['B0', 'B1', 'B2'], ['C0', 'C1', 'C2']]]
    

while expecting to get:

    
    
    new list 1 = [[['A0'], ['B0'], ['C0']], [['A1'], ['B1'], ['C1']], [['A2'], ['B2'], ['C2']]]
    

below is the code I used (sorry for the length, i'm sure there is a very
reduced version).

    
    
    def Main():
    
        array = ['A0', 'A1', 'A2', 'B0', 'B1', 'B2', 'C1', 'C0', 'C2']
    
        list1 = NewList1(array)
        print 'new list 1 = ' + str(list1)
    
        list2 = NewList2(list1)
        print 'new list 2 = ' + str(list2)
    
    def NewList1(thelist):
        sublist1=[]
        sublist2=[]
        sublist3=[]
        sublist4=[]
        newlist=[]
        for item in thelist:
            if hasattr(item,"__iter__"):
                for subitem in item:
                    sublist1.append(subitem)
            elif "0" in item:
                    sublist2.append(item)
            elif "1" in item:
                    sublist3.append(item)
            elif "2" in item:
                    sublist4.append(item)
            else:
                newlist.append(item)
        if sublist2: newlist.append(sublist2)
        if sublist3: newlist.append(sublist3)
        if sublist4: newlist.append(sublist4)
    
        if sublist1: newlist.append(NewList1(sublist1))
    
        return newlist
    
    
    def NewList2(thelist):
        sublist1=[]
        sublist2=[]
        sublist3=[]
        sublist4=[]
        newlist=[]
        for item in thelist:
            if hasattr(item,"__iter__"):
                for subitem in item:
                    sublist1.append(subitem)
            elif "A" in item:
                    sublist2.append(item)
            elif "B" in item:
                    sublist3.append(item)
            elif "C" in item:
                    sublist4.append(item)
            else:
                newlist.append(item)
        if sublist2: newlist.append(sublist2)
        if sublist3: newlist.append(sublist3)
        if sublist4: newlist.append(sublist4)
    
        if sublist1: newlist.append(NewList2(sublist1))
    
        return newlist
    
    Main();
    


Answer: Sort on your grouping criteria, then use
[`itertools.groupby()`](http://docs.python.org/2/library/itertools.html#itertools.groupby):

    
    
    from itertools import groupby
    
    key = lambda el: el[1]
    list2 = [[[el] for el in grouped] for group, grouped in groupby(sorted(input, key=key), key)]
    

Demonstration:

    
    
    >>> from itertools import groupby
    >>> input = ['A0', 'A1', 'A2', 'B0', 'B1', 'B2', 'C1', 'C0', 'C2']
    >>> key = lambda el: el[1]
    >>> [[[el] for el in grouped] for group, grouped in groupby(sorted(input, key=key), key)]
    [[['A0'], ['B0'], ['C0']], [['A1'], ['B1'], ['C1']], [['A2'], ['B2'], ['C2']]]
    

Your output requirement is a little convoluted; if returning lists of the
grouped elements is enough, calling `list()` on `grouped` is enough:

    
    
    >>> [list(grouped) for group, grouped in groupby(sorted(input, key=key), key)]
    [['A0', 'B0', 'C0'], ['A1', 'B1', 'C1'], ['A2', 'B2', 'C2']]
    

