# Convert 2D python list of communities to VertexCluster

**URL:** <https://igraph.discourse.group/t/convert-2d-python-list-of-communities-to-vertexcluster/106>\
**Category:** Usage\
**Tags:** Python\
**Created:** [24 February 2020 21:37 UTC](https://igraph.discourse.group/t/convert-2d-python-list-of-communities-to-vertexcluster/106 "2020-02-24T21:37:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vedant\_Desai](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vedant_desai/32/146_2.png) [@Vedant\_Desai](https://igraph.discourse.group/u/Vedant_Desai)\
**Post date:** [24 February 2020 21:37 UTC](https://igraph.discourse.group/t/convert-2d-python-list-of-communities-to-vertexcluster/106/1 "2020-02-24T21:37:42Z")

</div>

I have a file containing the nodes of a graph and their respective communities using infomap community detection algorithm. I have read the file line by line and converted it into a 2D python list, where the list[0] gives you all the nodes in the 0th community and so on. Now to compare this infomap algorithm with the one provided in igraph using the compare\_communities (from igraph) I need both the community outputs in a vertexCluster object. How can I convert the 2D list to a vertexClustering Object?

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [24 February 2020 22:33 UTC](https://igraph.discourse.group/t/convert-2d-python-list-of-communities-to-vertexcluster/106/2 "2020-02-24T22:33:21Z")

</div>

You can simply [initalise](https://igraph.org/python/doc/igraph.clustering.VertexClustering-class.html# __init__ ) a new `VertexClustering` object. The only thing is that you need to supply a membership list, instead of a list of a list of clusters.

Let us assume that `clusters` is the list of clusters, such that `clusters[i]` is a list of nodes that belong to cluster `i`, and `graph` contains the appropriate graph. One way to convert `clusters` to a membership list is as follows

```auto
membership = [None]*graph.vcount()
for c, cluster in enumerate(clusters):
  for v in cluster:
    membership[v] = c

```

you can then create a new `VertexClustering` using

```auto
clustering = VertexClustering(graph, membership=membership)

```

If there are any nodes that are incorrectly labelled (i.e. not between `0` and `graph.vcount()-1`), this should throw an error.
