Thanks all. I was playing around with this and came up with another solution using dictionaries (... comfort zone ...!!)
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Code >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> from operator import itemgetter def group_stuff(data): group_dic = {} group_id = 0 for line in data: i = 0 for ref in line: if group_dic.get(ref) is None: if group_dic.get(line[1-i]) is not None: group_id = group_dic[line[1-i]] else: group_id +=1 group_dic[ref] = group_id i+=1 group_list = [] for id, group in sorted(group_dic.items(), key=itemgetter(1,0)): group_list.append((group, id)) return group_list if __name__ == '__main__': data = [('a','b'),('a','c'),('a','d'),('b','c'),('b','d'), ('c','d'),('e','f'),('e','g'),('f','g'),('h','i')] grouped = group_stuff(data) print grouped <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Code >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Output: [(1, 'a'), (1, 'b'), (1, 'c'), (1, 'd'), (2, 'e'), (2, 'f'), (2, 'g'), (3, 'h'), (3, 'i')] -- http://mail.python.org/mailman/listinfo/python-list