if I have a list of lists, that goes like this: [[0,['a','b']],[0,['c','d']],[3,['f','g']], [0,['a','b']],[0,['c','d']], [3,['f1','f2']], [2,['zz','dd']]]<br><br>what could be the best way to reorder this such that the sublists with the same first element go into their own sub-list ?<br><br>like, <br><br>sublist0 = [0,['a','b']],[0,['c','d']],[0,['a','b']],[0,['c','d']]<br><br>sublist2 = [2,['zz','dd']]<br><br>sublist3 [3,['f','g']], [3,['f1','f2']]<br><br>Does this seem to be the best way to do it ?<br><br>===============================================================================<br><br>from operator import itemgetter<br>from itertools import groupby<br><br>data_list = [[0,['a','b']],[0,['c','d']],[3,['f','g']], [0,['a','b']],[0,['c','d']], [3,['f1','f2']], [2,['zz','dd']]]<br>data_list.sort()<br><br>list1 = []<br><br>for key, items in groupby(data_list, key= itemgetter(0)):&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp;
 list1.append(list(items))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br>print "After grouping the list by first value in each element:"<br>print list1<br><br>print "printing each sublist"<br>for i in list1:<br>&nbsp;&nbsp;&nbsp; print i<br>&nbsp;&nbsp; &nbsp;<br><br>===============================================================================<br><br>output :<br><br>After grouping the list by first value in each element:After grouping the list by first value in each element:<br>[[[0, ['a', 'b']], [0, ['a', 'b']], [0, ['c', 'd']], [0, ['c', 'd']]], [[2, ['zz', 'dd']]], [[3, ['f', 'g']], [3, ['f1', 'f2']]]]<br>printing each sublist<br>[[0, ['a', 'b']], [0, ['a', 'b']], [0, ['c', 'd']], [0, ['c', 'd']]]<br>[[2, ['zz', 'dd']]]<br>[[3, ['f', 'g']], [3, ['f1', 'f2']]]<br><br><p>&#32;
      <hr size=1>Got a little couch potato? <br>
Check out fun <a href="http://us.rd.yahoo.com/evt=48248/*http://search.yahoo.com/search?fr=oni_on_mail&p=summer+activities+for+kids&cs=bz">summer activities for kids.</a>