I\'m doing this but it feels this can be achieved with much less code. It is Python after all. Starting with a list, I split that list into subsets based on a string prefix.
Use itertools' groupby:
itertools
def get_field_sub(x): return x.split('_')[1] mylist = sorted(mylist, key=get_field_sub) [ (x, list(y)) for x, y in groupby(mylist, get_field_sub)]