# Python-igraph: Creating a graph object with a list of nodes

**URL:** <https://igraph.discourse.group/t/python-igraph-creating-a-graph-object-with-a-list-of-nodes/621>\
**Category:** Usage\
**Tags:** Python\
**Created:** [8 February 2021 16:17 UTC](https://igraph.discourse.group/t/python-igraph-creating-a-graph-object-with-a-list-of-nodes/621 "2021-02-08T16:17:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![graphie](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/graphie/32/449_2.png) [@graphie](https://igraph.discourse.group/u/graphie)\
**Post date:** [8 February 2021 16:17 UTC](https://igraph.discourse.group/t/python-igraph-creating-a-graph-object-with-a-list-of-nodes/621/1 "2021-02-08T16:17:22Z")

</div>

Hi all!

I am new to Python and even newer to iGraph.

I have a project which I have been stuck on for a while (please bear with me…)

I wish to generate a tree structure to visualize the structure of an XML file with Plotly-igraph.

I am using this as reference:  
[reference link](https://plotly.com/python/tree-plots/)

 ![image](https://global.discourse-cdn.com/free1/uploads/igraph/original/1X/e76e70bd624b994afa9d88ddc1fdb36107668dc5.png)

Trouble is, I am unsure how to do so.

My current implementation entails the following steps:

1. Getting all the elements of the XML file and putting it in to a list.  
(elements include 3 strings: tag, attribute, text)

2. Using the list to generate the tree in igraph.

However, I am unsure how to do the 2nd part…  
igraph does not seem to accept objects as arguments when I try to do a:

> G = Graph()  
> for x in elementlist:  
> G.add\_vertices(x)

giving the following error:

> an integer is required (got type treenode)

**As such, my 2 main questions are:**  
**1. How do I use a list to generate a graph object?**  
**2. I also have no idea how to get the edges between each node in an XML.**

Any help with the first question will be greatly appreciated and it would be nice if anyone has suggestions for the 2nd question! (I’m open to changing my current implementation)

---

<div class="post-metadata">

**Author:** ![Max\_Schommer](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/max_schommer/32/361_2.png) [@Max\_Schommer](https://igraph.discourse.group/u/Max_Schommer)\
**Post date:** [9 February 2021 01:12 UTC](https://igraph.discourse.group/t/python-igraph-creating-a-graph-object-with-a-list-of-nodes/621/2 "2021-02-09T01:12:25Z")

</div>

Hi graphie,

So `add_vertices` takes an integer number because it wants to add that number of vertices to the graph. What you might want is the method called `add_vertex`. This accepts another parameter `name` which can then be used to find the vertex and add data to it.

```auto
G = Graph()
for x in elementlist:
    G.add_vertex(name="some_name")
    G.vs.find(name="some_name")["x"] = x

```

See [https://igraph.org/python/doc/igraph.Graph-class.html#add\_vertex](https://igraph.org/python/doc/igraph.Graph-class.html#add_vertex) for more information.  
As for your second question, maybe someone more knowledgeable in XML parsing could point you in the right direction?
