Skip to main content
`Ctrl`+`K`

# MultiGraph.add_edges_from

MultiGraph.add_edges_from(ebunch_to_add, **attr)

  Add all the edges in ebunch_to_add.

  Parameters:
    ebunch_to_add container of edges

      Each edge given in the container will be added to the graph. The edges can be:

      > - 2-tuples (u, v) or

      > - 3-tuples (u, v, d) for an edge data dict d, or

      > - 3-tuples (u, v, k) for not iterable key k, or

      > - 4-tuples (u, v, k, d) for an edge with data and key k

    attr keyword arguments, optional

      Edge data (or labels or objects) can be assigned using keyword arguments.

  Returns:
    A list of edge keys assigned to the edges in `ebunch`.

  See also

  `add_edge`

    add a single edge

  `add_weighted_edges_from`

    convenient way to add weighted edges

  Notes

  Adding the same edge twice has no effect but any edge data will be updated when each duplicate edge is added.

  Edge attributes specified in an ebunch take precedence over attributes specified via keyword arguments.

  Default keys are generated using the method `new_edge_key()`. This method can be overridden by subclassing the base class and providing a custom `new_edge_key()` method.

  When adding edges from an iterator over the graph you are changing, a `RuntimeError` can be raised with message: `RuntimeError: dictionary changed size during iteration`. This happens when the graph’s underlying dictionary is modified during iteration. To avoid this error, evaluate the iterator into a separate object, e.g. by using `list(iterator_of_edges)`, and pass this object to `G.add_edges_from`.

  Examples

      >>> G = nx.Graph()  # or DiGraph, MultiGraph, MultiDiGraph, etc
      >>> G.add_edges_from([(0, 1), (1, 2)])  # using a list of edge tuples
      >>> e = zip(range(0, 3), range(1, 4))
      >>> G.add_edges_from(e)  # Add the path graph 0-1-2-3

  Associate data to edges

      >>> G.add_edges_from([(1, 2), (2, 3)], weight=3)
      >>> G.add_edges_from([(3, 4), (1, 4)], label="WN2898")

  Evaluate an iterator over a graph if using it to modify the same graph

      >>> G = nx.MultiGraph([(1, 2), (2, 3), (3, 4)])
      >>> # Grow graph by one new node, adding edges to all existing nodes.
      >>> # wrong way - will raise RuntimeError
      >>> # G.add_edges_from(((5, n) for n in G.nodes))
      >>> # right way - note that there will be no self-edge for node 5
      >>> assigned_keys = G.add_edges_from(list((5, n) for n in G.nodes))