# python iterator of connected vertex

**URL:** https://igraph.discourse.group/t/python-iterator-of-connected-vertex/561
**Category:** Usage
**Tags:** Python
**Created:** [23 December 2020 13:21 UTC](https://igraph.discourse.group/t/python-iterator-of-connected-vertex/561 "2020-12-23T13:21:44Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![vadims06](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vadims06/32/418_2.png) [@vadims06](https://igraph.discourse.group/u/vadims06)
#### Post date: [23 December 2020 13:21 UTC](https://igraph.discourse.group/t/python-iterator-of-connected-vertex/561/1 "2020-12-23T13:21:44Z")

</div>

Hi folks,

I would be glad for any suggestion about how to build an iterator, which yields pairs of adjacent (connected) nodes? The same as it’s printed in summary(graph)

---

<div class="post-metadata">

### Author: ![tamas](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/tamas/32/1261_2.png) [@tamas](https://igraph.discourse.group/u/tamas)
#### Post date: [28 December 2020 10:22 UTC](https://igraph.discourse.group/t/python-iterator-of-connected-vertex/561/2 "2020-12-28T10:22:37Z")

</div>

You can simply iterate over the connected pairs of vertices by iterating over the `es` attribute of a `Graph` object:

```python
for edge in graph.es:
    print(edge.source, edge.target)

```

Use `help(EdgeSeq)` to find out more about the edge sequence object and `help(Edge)` to read more about the individual properties of edges.

---

<div class="post-metadata">

### Author: ![vadims06](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vadims06/32/418_2.png) [@vadims06](https://igraph.discourse.group/u/vadims06)
#### Post date: [28 December 2020 10:55 UTC](https://igraph.discourse.group/t/python-iterator-of-connected-vertex/561/3 "2020-12-28T10:55:29Z")

</div>

Thank you @tamas. Could you please say how complex graph.es.select(\_source, \_target) or graph.es.select(between=) query?

---

<div class="post-metadata">

### Author: ![tamas](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/tamas/32/1261_2.png) [@tamas](https://igraph.discourse.group/u/tamas)
#### Post date: [28 December 2020 14:11 UTC](https://igraph.discourse.group/t/python-iterator-of-connected-vertex/561/4 "2020-12-28T14:11:59Z")

</div>

`_source`, `_target` and `_between` use the `incident()` method of the graph to query the edges incident on a particular vertex. Due to the indexed edge list data structure that the igraph library uses internally, a single call to `incident()` should run in constant time, therefore the time needed to execute a `_source`, `_target` or `_between` query should be proportional to the number of vertices involved, plus the complexity of the set intersection or union operations, which rely on standard Python data structures so their time complexity is ultimately up to the Python implementation that you use (but they should be heavily optimized).

However, if you are looking for a single particular edge between two vertices, you are even better off with using `graph.get_eid(source, target)`, which returns the numeric edge ID, and then you can use that to index into `graph.es`:

```python
graph.es[graph.get_eid(source, target)]

```

---

<div class="post-metadata">

### Author: ![Simonas\_Mamaitis](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/simonas_mamaitis/32/115_2.png) [@Simonas\_Mamaitis](https://igraph.discourse.group/u/Simonas_Mamaitis)
#### Post date: [29 December 2020 00:00 UTC](https://igraph.discourse.group/t/python-iterator-of-connected-vertex/561/5 "2020-12-29T00:00:54Z")

</div>

> [@tamas](#):
>
> ```auto
> for edge in graph.es:
> print(edge.source, edge.target)
> 
> ```

I found that the fastest way to collect edges of graph is following:

```
import numpy as np
index = np.fromiter(chain(*graph.get_edgelist()), np.dtype('i'), count=graph.ecount())
edges = index.reshape(-1, 2) # or index.reshape(-1, 2).tolist()

```

It is equivalent of `np.array([n.tuple for n in graph.es])` or `np.array([(edge.source, edge.target) for n in graph.es])`

You can find more info in my [draft](https://github.com/loijord/Python/blob/master/graph_arcade.ipynb), diagram number 4.
