Sounds like itertools.groupby() is what you're looking for, just sort your sequence on the grouping func that you will pass as 2nd arg to groupby(), before you pass the sequence to groupby()
http://www.python.org/doc/2.4/whatsnew/node13.html http://www.python.org/dev/doc/devel/lib/itertools-functions.html -- http://mail.python.org/mailman/listinfo/python-list