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
(self, ebunch_to_add, **attr)
| 536 | return key |
| 537 | |
| 538 | def add_edges_from(self, ebunch_to_add, **attr): |
| 539 | """Add all the edges in ebunch_to_add. |
| 540 | |
| 541 | Parameters |
| 542 | ---------- |
| 543 | ebunch_to_add : container of edges |
| 544 | Each edge given in the container will be added to the |
| 545 | graph. The edges can be: |
| 546 | |
| 547 | - 2-tuples (u, v) or |
| 548 | - 3-tuples (u, v, d) for an edge data dict d, or |
| 549 | - 3-tuples (u, v, k) for not iterable key k, or |
| 550 | - 4-tuples (u, v, k, d) for an edge with data and key k |
| 551 | |
| 552 | attr : keyword arguments, optional |
| 553 | Edge data (or labels or objects) can be assigned using |
| 554 | keyword arguments. |
| 555 | |
| 556 | Returns |
| 557 | ------- |
| 558 | A list of edge keys assigned to the edges in `ebunch`. |
| 559 | |
| 560 | See Also |
| 561 | -------- |
| 562 | add_edge : add a single edge |
| 563 | add_weighted_edges_from : convenient way to add weighted edges |
| 564 | |
| 565 | Notes |
| 566 | ----- |
| 567 | Adding the same edge twice has no effect but any edge data |
| 568 | will be updated when each duplicate edge is added. |
| 569 | |
| 570 | Edge attributes specified in an ebunch take precedence over |
| 571 | attributes specified via keyword arguments. |
| 572 | |
| 573 | Default keys are generated using the method ``new_edge_key()``. |
| 574 | This method can be overridden by subclassing the base class and |
| 575 | providing a custom ``new_edge_key()`` method. |
| 576 | |
| 577 | When adding edges from an iterator over the graph you are changing, |
| 578 | a `RuntimeError` can be raised with message: |
| 579 | `RuntimeError: dictionary changed size during iteration`. This |
| 580 | happens when the graph's underlying dictionary is modified during |
| 581 | iteration. To avoid this error, evaluate the iterator into a separate |
| 582 | object, e.g. by using `list(iterator_of_edges)`, and pass this |
| 583 | object to `G.add_edges_from`. |
| 584 | |
| 585 | Examples |
| 586 | -------- |
| 587 | >>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc |
| 588 | >>> G.add_edges_from([(0, 1), (1, 2)]) # using a list of edge tuples |
| 589 | >>> e = zip(range(0, 3), range(1, 4)) |
| 590 | >>> G.add_edges_from(e) # Add the path graph 0-1-2-3 |
| 591 | |
| 592 | Associate data to edges |
| 593 | |
| 594 | >>> G.add_edges_from([(1, 2), (2, 3)], weight=3) |
| 595 | >>> G.add_edges_from([(3, 4), (1, 4)], label="WN2898") |