# Loading data from a list of lists (representing edges) in a graph igraph in python

I have some relational data in a list of lists format that I would like to import into `iGraph.Graph()`

. The list of lists contains repeating edges, and ultimately I would like to add an edge for the repeating edges. However, I am currently unable to figure out what I am doing wrong when I am just trying to add edges to the graph, minus the weight.

I thought the problem was that I had to first import all the vertices into the graph and then add edges between the vertices, but that doesn't seem to be the case.

What am I doing wrong when loading these edges into the graph?

* How can I change the face loading process to first search for an edge in the graph, if not found, add an edge, and if found, increase the weight of that edge by 1, *

** DATA**

```
In [60]: edges
Out[60]: [['a', 'b'],
['a', 'b'],
['a', 'b'],
['b', 'a'],
['a', 'c'],
['c', 'a'],
['c', 'd'],
['c', 'd'],
['d', 'c'],
['d', 'c']]
```

** NEEDLE CODE FOR EDGE LOADING AND ERROR**

```
In [61]: # extract vertices for edges list
vertices = []
for line in edges:
nodes.append(line[0])
nodes.append(line[1])
# find unique vertices
uniq_vertices = set(sorted(nodes))
In [62]: # create an empty graph
g = igraph.Graph()
In [63]: # add vertices to the graph
g.add_vertices(uniq_vertices)
In [64]: # for each line in the edges list, check to see if it already in the graph, and if not, add it to the graph.
for line in edges:
if not line in g.get_edgelist():
g.add_edges(edges)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-64-04a376c78860> in <module>()
2 for line in edges:
3 if not line in g.get_edgelist():
----> 4 g.add_edges(edges)
C:\Users\Curtis\Anaconda\lib\site-packages\igraph\__init__.pyc in add_edges(self, es)
228 endpoints. Vertices are enumerated from zero.
229 """
--> 230 return GraphBase.add_edges(self, es)
231
232 def add_vertex(self, name=None, **kwds):
ValueError: no such vertex: 'a'
```

source to share

The problem is probably here (at least this piece of code doesn't make sense to me):

```
for line in edges:
if not line in g.get_edgelist():
g.add_edges(edges)
```

This is where you check if is `line`

in `g.get_edgelist()`

, and rest assured that this is not because your list `edges`

contains lists, but `g.get_edgelist()`

returns tuples instead. Then you add all the edges, not just the ones you checked.

I am adding an alternate version of your code that seems to do the job for me. Note that I am not eliminating multiple edges when I create the graph - I add them and then just ask the igraph to collapse them into one and add their weights together:

```
import igraph
edges = [['a', 'b'],
['a', 'b'],
['a', 'b'],
['b', 'a'],
['a', 'c'],
['c', 'a'],
['c', 'd'],
['c', 'd'],
['d', 'c'],
['d', 'c']]
# collect the set of vertex names and then sort them into a list
vertices = set()
for line in edges:
vertices.update(line)
vertices = sorted(vertices)
# create an empty graph
g = igraph.Graph()
# add vertices to the graph
g.add_vertices(vertices)
# add edges to the graph
g.add_edges(edges)
# set the weight of every edge to 1
g.es["weight"] = 1
# collapse multiple edges and sum their weights
g.simplify(combine_edges={"weight": "sum"})
```

source to share