neighbors

Graph.neighbors(n)[source]

Return a list of the nodes connected to the node n.

Parameters:n (node) – A node in the graph
Returns:nlist – A list of nodes that are adjacent to n.
Return type:list
Raises:NetworkXError – If the node n is not in the graph.

Notes

It is usually more convenient (and faster) to access the adjacency dictionary as G[n]:

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_edge('a','b',weight=7)
>>> G['a']
{'b': {'weight': 7}}

Examples

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.neighbors(0)
[1]