On Sun, Jul 29, 2012 at 4:29 PM, Peter Otten <__pete...@web.de> wrote: > > If you have to sort your data anyway you can sort it first and then apply > itertools.groupby()...
That was my thinking. I wrote it with groupby (see link below) because Francesco described field 0 as "in growing order". http://mail.python.org/pipermail/tutor/2012-July/090607.html I see now the 2nd call to sorted() in the tuple generator was redundant. I should have just sorted the data without the key function to order within each group by field 4 in the first pass: keyfunc = itemgetter(0,1,2,3) groups = groupby(sorted(data), keyfunc) result = [] for group, records in groups: temp = tuple('%s/%s' % r[5:] for r in records) result.append(group + temp) In general though you have to sort and group by the same key. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor