G = nx.Graph()
G.add_edges_from([('v1', 'v2'), ('v2', 'v4'), ('v1', 'v3')])

def neighborhood(G, node, n):
    path_lengths = nx.single_source_dijkstra_path_length(G, node)
return [node for node, length in path_lengths.items() if length == n]
print(neighborhood(G, 'v1', 1))
print(neighborhood(G, 'v1', 2))
Finding the n-degree neighborhood of a node