Split nested list

Hi all,

I have a nested list like:
[ [aa,0] , [bb,1] , [cc,0] , [dd,2] , [ee,1] ]

is there a nice short code to separate this into new nested list where the result would look like this:
[ [ [aa,0] , [cc,0] ] , [ [bb,1] , [ee,1] ] , [ [dd,2] ] ]

Meaning that the second instance in list is the rule for the new nested list.
I hope this is understandable.
The nested list could for sure be much longer then described above.

all help is much appreciated!
Thanks in advance


How about this:

input = [ ['aa',0] , ['bb',1] , ['cc',0] , ['dd',2] , ['ee',1] ]

new_list = []
while input:
    if len(input)>=2:
        new_list.append([input.pop(0), input.pop(1)])

print new_list


Would a dictionary be OK as an output? You would index the results in the same way as with a list

You have both solutions below…

from collections import defaultdict

dict_out = defaultdict(list)
list_in = [ ['aa',0] , ['bb',1] , ['cc',0] , ['dd',2] , ['ee',1] ]
for item in list_in:

list_out = [dict_out[i] for i in sorted(dict_out.keys())]

Thanks you for both of the solutions!
Helped allot.

Best regards