def next(self):

        # check if all iterators exhausted
        if not any(self.it):
            raise StopIteration

        # yield all that correspond to target key and update
        result = []
        targetkey = self.targetkey
        updated = 0

        for x, y in enumerate(self.current):
            key, val = y
            if key and key == targetkey:
                # save result (instantiate to prevent overwriting)
                # in next method
                result.append(list(val))
                # advance
                try:
                    self.current[x] = next(self.it[x])
                except __HOLE__:
                    self.it[x] = None
                    self.current[x] = (None, None)
                updated += 1
            else:
                # return empty result
                result.append([])

        assert updated > 0, "no updates - infinite loop"

        # decide which is target key
        try:
            self.targetkey = min([x[0]
                                  for x in self.current if x[0] is not None])
        except ValueError:
            # if all are None, sequence is empty
            self.targetkey = None

        return targetkey, result
dataset/ETHPy150Open CGATOxford/cgat/scripts/diff_bam.py/multiway_groupby.next