def iter_nearest_neighbours(umis, substr_idx):
    """
    Use substring dict to get (approximately) all the nearest neighbours to
    each in a set of UMIs.
    """
    for u in umis:
        neighbours = set()
        for idx, substr_map in substr_idx.items():
            u_sub = u[slice(*idx)]
            neighbours = neighbours.union(substr_map[u_sub])
        neighbours.remove(u)
        for nbr in neighbours:
            yield u, nbr
