Warning

This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.

networkx.readwrite.adjlist.parse_adjlist

parse_adjlist(lines, comments='#', delimiter=None, create_using=None, nodetype=None)[source]

Parse lines of a graph adjacency list representation.

Parameters:
  • lines (list or iterator of strings) – Input data in adjlist format
  • create_using (NetworkX graph constructor, optional (default=nx.Graph)) – Graph type to create. If graph instance, then cleared before populated.
  • nodetype (Python type, optional) – Convert nodes to this type.
  • comments (string, optional) – Marker for comment lines
  • delimiter (string, optional) – Separator for node labels. The default is whitespace.
Returns:

G – The graph corresponding to the lines in adjacency list format.

Return type:

NetworkX graph

Examples

>>> lines = ['1 2 5',
...          '2 3 4',
...          '3 5',
...          '4',
...          '5']
>>> G = nx.parse_adjlist(lines, nodetype=int)
>>> nodes = [1, 2, 3, 4, 5]
>>> all(node in G for node in nodes)
True
>>> edges = [(1, 2), (1, 5), (2, 3), (2, 4), (3, 5)]
>>> all((u, v) in G.edges() or (v, u) in G.edges() for (u, v) in edges)
True

See also

read_adjlist()